Page 1 of 1
str_replace
Posted: Thu Jun 22, 2006 2:15 pm
by tail
I'm trying to replace "<" with "<". I'm using this code but it doesn't work.
Code: Select all
<form action="replace.php" method="post">
<textarea cols="50" rows="15" name="code" class="button"></textarea>
<input type="submit" value="Submit" class="button">
<?php
str_replace("<", "<", "");
echo $_POST["code"];
?>
Any help is appreciated. Thanks.
Posted: Thu Jun 22, 2006 2:20 pm
by s.dot
Code: Select all
echo str_replace('<','<',$_POST['code']);
Posted: Thu Jun 22, 2006 11:05 pm
by tail
That didn't work either...
Posted: Thu Jun 22, 2006 11:08 pm
by tecktalkcm0391
Try:
Code: Select all
<?php
$value = str_ireplace("<", "<", $_POST['code']);
echo $value
?>
Posted: Thu Jun 22, 2006 11:45 pm
by Luke
you could just use htmlentities(), but it will turn everything it can into their html entities and not just the ancle brackets... just FYI if you weren't aware

Posted: Fri Jun 23, 2006 3:24 am
by s.dot
TheNinjaSpaceGoat wrote:you could just use htmlentities(), but it will turn everything it can into their html entities and not just the ancle brackets... just FYI if you weren't aware

I think he's trying to do the opposite

in which case he'd want html_entity_decode()
Posted: Fri Jun 23, 2006 6:58 am
by tail
I got it.
Code: Select all
<?php
$find = array("<",">");
$replace = array("<",">");
$arr = $_GET["code"];
print_r(str_replace($find,$replace,$arr));
?>
Posted: Fri Jun 23, 2006 1:33 pm
by s.dot
oh, you were doing it backwards.
Code: Select all
echo str_replace(array('<','>'),array('<','>'),$_POST['code']);
or
Code: Select all
echo htmlentities($_POST['code'], ENT_QUOTES);
Posted: Fri Jun 23, 2006 7:35 pm
by tail
Ok I'm having another problem. I want to have a checkbox that the user can select and if it is checked it displays a code. Heres the form:
Code: Select all
<input name="code" type="checkbox" value="yes">
Here's the action:
Code: Select all
<?php
$find = array("<",">");
$replace = array("<",">");
$arr = $_POST["code"];
$code = str_replace($find,$replace,$arr);
$codes="<textarea cols=50 rows=15 class=button>$code</textarea>";
if ($_POST['code'] == "yes")
echo("$codes");
else
echo("");
?>
Posted: Fri Jun 23, 2006 8:25 pm
by feyd
what's the problem?
Posted: Fri Jun 23, 2006 8:26 pm
by tail
Posted: Fri Jun 23, 2006 8:29 pm
by feyd
Follow the logic of how $code comes to exist.
Posted: Fri Jun 23, 2006 8:39 pm
by tail
Lost...

Posted: Fri Jun 23, 2006 8:48 pm
by feyd
You've placed $code in your <textarea>. $code is the returned data from str_replace() which in turn derives from $arr. $arr is set to $_POST['code']. $_POST['code'] is your checkbox.
Posted: Fri Jun 23, 2006 9:25 pm
by tail
How would I get a large HTML code for that?