Page 1 of 1

Replace character in block of text

Posted: Mon Aug 07, 2006 3:37 am
by tail
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

Posted: Mon Aug 07, 2006 4:07 am
by spikeZ
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