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
tail
Forum Commoner
Posts: 66 Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ
Post
by tail » Mon Aug 07, 2006 3:37 am
I'm trying to replace all the " in a block of text with '. I'm using this code:
Code: Select all
<?php
$code=$_POST["code"];
if ($code != "")
{
$before = array("/"/");
$after = array("'");
$replace = str_replace($before, $after, $code);
echo "<textarea cols='40' rows='10'>$replace></textarea>";
}
else
{
echo "<form action='replace.php' method='post'>";
echo "Code: <br><textarea cols='40' rows='10' name='code'></textarea><br>";
echo "<input type='submit' value='Submit'>";
echo "</form>";
}
?>
I get this error:
Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' in /home3/perks/public_html/replace.php on line 11
Any help is much appreciated
spikeZ
Forum Newbie
Posts: 2 Joined: Tue Jun 06, 2006 2:54 pm
Location: Manchester, UK
Post
by spikeZ » Mon Aug 07, 2006 4:07 am
You have mis-escaped the quotes, also if you are only replacing one character there is no need for an array setup:
Code: Select all
<?php
$code=$_POST["code"];
if ($code != "")
{
$replace = str_replace(" \"", " \' ", $code);
echo "<textarea cols='40' rows='10'>$replace></textarea>";
}
else
{
echo "<form action='replace.php' method='post'>";
echo "Code: <br><textarea cols='40' rows='10' name='code'></textarea><br>";
echo "<input type='submit' value='Submit'>";
echo "</form>";
}
?>
Cheers
Spike