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
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

str_replace

Post by SidewinderX »

hello im using the function str_replace but each time it finds something like href="/images/image.gif i want it to be be replaced with href="$_POST[url]/images/image.gif

i tried:

Code: Select all

<?php
$srch = array('href="/', 'src="/');
$rplc = array('href="/$_POST[url]/', 'src="/$_POST[url]/');
$go = str_replace($srch, $rplc, $content);
echo $go;
?>
but all of the ( " ) and ( ' ) mess everything up so instead of replacing $_POST['url'] with the correct URL it just replaces it with $_POST[url] so it ends up looking like
src="/$_POST[url]/images/logo.gif"
any ideas how to correct this?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

you have to escape the single quotes, because they will treat the text as a string

Code: Select all

<?php

$srch = array('href="/', 'src="/');
$rplc = array('href="/'.$_POST['url'].'/', 'src="/'.$_POST['url'].'/');
$go = str_replace($srch, $rplc, $content);

echo $go;

?>
Post Reply