473,326 Members | 2,061 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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 2691
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******@sprynet.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
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...
3
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'...
1
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...
7
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...
3
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 ...
8
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...
2
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); ...
5
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...
1
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.