str_replace

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
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

str_replace

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

echo str_replace('<','<',$_POST['code']);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

That didn't work either...
User avatar
tecktalkcm0391
DevNet Resident
Posts: 1030
Joined: Fri May 26, 2006 9:25 am
Location: Florida

Post by tecktalkcm0391 »

Try:

Code: Select all

<?php 
$value = str_ireplace("<", "<", $_POST['code']);
echo $value
?>
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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 :wink:
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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 :wink:
I think he's trying to do the opposite :-P
in which case he'd want html_entity_decode()
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

I got it.

Code: Select all

<?php
$find = array("<",">");
$replace = array("<",">");
$arr = $_GET["code"];
print_r(str_replace($find,$replace,$arr));
?>
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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);
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post 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("");
?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's the problem?
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

It's posting 'yes' in the textarea.

Here: http://perks.mypimpedlayout.com/index.php?id=schedule
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Follow the logic of how $code comes to exist.
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

Lost... :?:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
tail
Forum Commoner
Posts: 66
Joined: Sat Oct 01, 2005 4:42 pm
Location: NJ

Post by tail »

How would I get a large HTML code for that?
Post Reply