Is this preg_replace correct? – PHP

  php

Q(Question):

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?

A(Answer):

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 =–

A(Answer):

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 =–

A(Answer):

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

A(Answer):

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?

A(Answer):

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 =–

A(Answer):

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

LEAVE A COMMENT