Page 1 of 1

php replace function Please help Newbie to PHP

Posted: Mon Sep 08, 2003 11:39 am
by aelaron
I need a function that will read the results of a page and replace all ' " ; with a NULL value. I saw this from the php funtion list of php.net:

$match=array("ONE","TWO","THREE");
$replace=array("TWO WORDS","MANY LETTERS","OTHER IDEAS");
$sample="ONE SAMPLE";
echo str_replace($match,$replace,$sample);

Is this correct? I think I need to escape the ' " and the ; but unsure what I need to do.

$match=array("'",""",";");
$replace=array("","","");
$sample="ONE SAMPLE";
echo str_replace($match,$replace,$sample);

Thanks,

Aelaron

Posted: Mon Sep 08, 2003 12:23 pm
by JAM

Code: Select all

<?php
//  ' " ;
$from = array('"',"'",";");
$to = array("","","");
$sample = 'This is ONE; wierd """" SAMPLE';
echo str_replace($from,$to,$sample);
?>
I used single quotes ( ' ) around double quotes ( " ), double around singlequotes...

Posted: Mon Sep 08, 2003 12:37 pm
by aelaron
Thanks Jam that worked for me!!

Aelaron

Posted: Mon Sep 08, 2003 12:38 pm
by sdibb
If you're trying to get rid of them, you can escape them like this:

Code: Select all

$string=str_replace(""","",$string);
$string=str_replace("''","",$string);

Posted: Tue Sep 09, 2003 4:30 am
by twigletmac
sdibb wrote:If you're trying to get rid of them, you can escape them like this:

Code: Select all

$string=str_replace(""","",$string);
$string=str_replace("''","",$string);
You don't need to escape single quotes within double quotes, so

Code: Select all

$string=str_replace("''","",$string);
should be

Code: Select all

$string=str_replace("'", '', $string);
or alternatively

Code: Select all

$string=str_replace('''', '',$string);
The same is true for double quotes within single quotes, so

Code: Select all

$string=str_replace(""","",$string);
could be:

Code: Select all

$string=str_replace('"', '', $string);
This is as shown in the example JAM posted.

Mac