Page 1 of 1

str_replace

Posted: Fri Sep 10, 2004 3:20 pm
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?

Posted: Fri Sep 10, 2004 3:50 pm
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;

?>