php replace function Please help Newbie to PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aelaron
Forum Newbie
Posts: 4
Joined: Sun Dec 29, 2002 5:35 pm

php replace function Please help Newbie to PHP

Post 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
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post 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...
aelaron
Forum Newbie
Posts: 4
Joined: Sun Dec 29, 2002 5:35 pm

Post by aelaron »

Thanks Jam that worked for me!!

Aelaron
sdibb
Forum Newbie
Posts: 5
Joined: Mon Sep 08, 2003 12:34 pm
Location: Orem, Utah, USA

Post 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);
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply