473,657 Members | 2,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is this preg_replace correct?

Just want to verify. I have a form and I want to parse any semi-colons
out of the submitted info and replace with commas. Would this be the
correct way to do it?

$question1 = preg_replace("; ", ",", $question1);

Is there a better way to replace it in all the $_POST variables at once
like maybe

$_POST[] - preg_replace("; ", ",", $_POST[]);

or would I have to do a foreach loop?

Jul 17 '05 #1
6 2715
JDJones wrote:
Just want to verify. I have a form and I want to parse any semi-colons
out of the submitted info and replace with commas. Would this be the
correct way to do it?

$question1 = preg_replace("; ", ",", $question1);
Ok, but IMHO str_replace would be a better choice.

Is there a better way to replace it in all the $_POST variables at once
like maybe

$_POST[] - preg_replace("; ", ",", $_POST[]);

or would I have to do a foreach loop?


I'd do it with a foreach loop.
But pay attention if you have arrays in your POST.

<?php
foreach ($_POST as $k=>$v) {
if (!is_array($v)) $_POST[$k] = str_replace(';' , ',', $v);
else /* deal with array */;
}
?>
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2
Pedro Graca wrote:
JDJones wrote:
$question1 = preg_replace("; ", ",", $question1);
Ok, but IMHO str_replace would be a better choice.


Well ... almost ok :)
it should be

$question1 = preg_replace("/;/", ",", $question1);
** sloppy me! **
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #3
Pedro Graca wrote:
Pedro Graca wrote:
JDJones wrote:
$question1 = preg_replace("; ", ",", $question1);


Ok, but IMHO str_replace would be a better choice.

(...)
it should be

$question1 = preg_replace("/;/", ",", $question1);
** sloppy me! **


But still, consider str_replace! :-)
You don't need a regex for this.

Rudi
Jul 17 '05 #4
Rudolf Horbas wrote:
Pedro Graca wrote:
JDJones wrote:

$question1 = preg_replace("; ", ",", $question1);

Ok, but IMHO str_replace would be a better choice.


(...)
it should be

$question1 = preg_replace("/;/", ",", $question1);
** sloppy me! **

But still, consider str_replace! :-)
You don't need a regex for this.


So would simply replacing preg_replace with str_replace be what I need?

$question1 = str_replace(";" , ",", $question);

or would I have to do something else with it?

Jul 17 '05 #5
JDJones wrote:
So would simply replacing preg_replace with str_replace be what I need?
Yes.
$question1 = str_replace(";" , ",", $question);

or would I have to do something else with it?


Nope. Unless you have /some/ semicolons you want to maintain.

If you don't mind transforming

horse;bird;"cat ;dog";elephant

into

horse,bird,"cat ,dog",elephant

the str_replace() is all you need.
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #6
JDJones <se******@spryn et.com> schrieb:
So would simply replacing preg_replace with str_replace be what I need?

$question1 = str_replace(";" , ",", $question);

or would I have to do something else with it?


No, that's all, but you might want to use the slightly faster version
$question1 = str_replace(';' , ',', $question);

Regards,
Matthias
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
4894
by: Juha Suni | last post by:
Hi! I have managed to live without using too much regular expressions so far, and now that I need one, I need some help too. I have a string containing a (possibly large) block of html. I need to insert code (an image to be precise) right after the last textual character in the string. This is, of course, no problem if the end contains plain text and no html. If the string ends in HTML however, I need to fiddle around. for example...
3
4317
by: TXSherry | last post by:
Hi, I cannot seem to wrap my brain around preg_replace. Though I've read the help file backwords and forwards. :/ Hoping someone can give me a solution here. Problem: Given string 'str' which may contain new lines and will contain html code, IN this string any "words" that begin with an underscore I want to replace with a given word. A word here being a group of chars preceded by a space or null (start of line) and closed by a...
1
2238
by: yawnmoth | last post by:
say i have the following script: <? $test = "aaaaa"; print '"' . preg_replace('/.*/','x',$test) . '"<br>'; $test = "\n\n\n\n\n"; print '"' . preg_replace('/.*/','x',$test) . '"'; ?> the output i would expect is as follows:
7
4993
by: Margaret MacDonald | last post by:
I've been going mad trying to figure out how to do this--it should be easy! Allow the user to enter '\_sometext\_', i.e., literal backslash, underscore, some text, literal backslash, underscore and, after submitting via POST to a preg_replace filter, get back '_sometext_' (i.e., the same thing with the literal backslashes stripped)
3
5321
by: Charles | last post by:
I'm new to this regular expression stuff. I'd like to use preg_replace to eliminate a known multi-line signature from the body of an E-mail. Say the body text is in $body, and the sig is this --- Sig line1 Sig line2 Sig line3 If I could just get rid of that, it would be pretty good. But I also get this
8
2363
by: erikcw | last post by:
Hi all, I'm trying to write a regex pattern to use in preg_replace. Basically I want to put around every line (\n) in this variable. However, I need to exclude lines that already have brackets or quotation marks around them as well as lines that start with a hyphen. Lastly the lines with ** need the brackets before the **, instead of at the end of the line. Here is what I have so far - it is close, but I keep ending up with
2
1532
by: Terence | last post by:
I've been puzzling over something for ages and now give up: Does anyone see any problem with any of the following: $line = preg_replace('/\{1,6})\]/i', "<font color='# ${1}'>", $line); $line = preg_replace('/\){3}((){3})?))\]/ i', "<font color='#${1}'>", $line);
5
2174
by: helraizer1 | last post by:
Hey folks and folksesses, I have written some code to get emoticons onto my image based shoutbox but there is quite a large bug, as there is with any code in the early stages, and I need your help to narrow it down for me. The idea is that the script pulls data from the database - username, colour, font and shout to print ' shout' onto the image. This part of the code all works perfectly. Then it looks through each line at a time to see if...
1
1958
by: minimatrix | last post by:
Hi, I am quite new to php but picking it up quite quickly, however I dont understand how a preg replace works. I am trying to count the number of columns selected but when I do the count I sometimes get +1 than what it should be. e.g. I have in my array A,B,C the count would return 3 which is correct but I have my data like this A,B,C, this adds 1 to the count because there is a comma on the end. I jus need to know how to do a...
0
8310
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6166
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.