darkfact Posted October 15, 2010 Share Posted October 15, 2010 The first two work perfectly. In the third, I tried to put them together in an array and nothing happens. Can anyone see what I am doing wrong? Thanks <?php $ident = $_POST['ident']; ?> <?php $filename = 'r5include.html'; $string = file_get_contents($filename); $data = str_replace("add.html' target='_blank'","$ident.html' target='iframe2'","$string"); $handle = fopen($filename, "w+"); fwrite($handle,$data); fclose($handle); ?> <?php $ident = $_POST['ident']; ?> <?php $ident2 = ($ident . 2); $filename = 'r5include.html'; $string = file_get_contents($filename); $data = str_replace("'images/up.jpg'","'$ident/$ident2.jpg'","$string"); $handle = fopen($filename, "w+"); fwrite($handle,$data); fclose($handle); ?> <?php $ident = $_POST['ident']; ?> <?php $ident2 = ($ident . 2); $filename = 'r5include.html'; $string = file_get_contents($filename); $old = array("'images/up.jpg'","add.html' target='_blank'"); $new = array("'$ident/$ident2.jpg'","$ident.html' target='iframe2'"); $data = str_replace("$old","$new","$string"); $handle = fopen($filename, "w+"); fwrite($handle,$data); fclose($handle); ?> Quote Link to comment https://forums.phpfreaks.com/topic/215963-problem-with-str_replace-array/ Share on other sites More sharing options...
taquitosensei Posted October 15, 2010 Share Posted October 15, 2010 I don't think it works quite like that. From the manual. $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); $onlyconsonants = str_replace($vowels, "", "Hello World of PHP"); I'm pretty sure you'd have to do each of the three separately in your 3rd example. Quote Link to comment https://forums.phpfreaks.com/topic/215963-problem-with-str_replace-array/#findComment-1122602 Share on other sites More sharing options...
kenrbnsn Posted October 15, 2010 Share Posted October 15, 2010 Can you give us an example of what's in the file r5include.html? Ken Quote Link to comment https://forums.phpfreaks.com/topic/215963-problem-with-str_replace-array/#findComment-1122603 Share on other sites More sharing options...
darkfact Posted October 15, 2010 Author Share Posted October 15, 2010 Thanks for the reply, this is the entire r5include file: <img height=75 src='images/up.jpg' hidefocus='true' usemap='#record51' /> <map name='record51'> <area shape='rect' coords='0,0,75,75' href='add.html' target='_blank'> If I use either of the first two that I posted it changes the way i want it to. If I put them both on the same page separately it changes the whole thing to what I want. But the one where I put them in an array doesn't do anything. Quote Link to comment https://forums.phpfreaks.com/topic/215963-problem-with-str_replace-array/#findComment-1122604 Share on other sites More sharing options...
DavidAM Posted October 16, 2010 Share Posted October 16, 2010 This line is incorrect: $data = str_replace("$old","$new","$string"); By enclosing $old and $new in quotes, you are forcing PHP to convert the arrays to strings, which will be the word "array". It is not necessary, and in fact sometimes hurtful, to enclose a variable in quotes when assigning it or passing it to a function. Change the line to: $data = str_replace($old, $new, $string); Quote Link to comment https://forums.phpfreaks.com/topic/215963-problem-with-str_replace-array/#findComment-1122633 Share on other sites More sharing options...
darkfact Posted October 16, 2010 Author Share Posted October 16, 2010 Thanks for the solution and explanation. It now works perfectly. Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/215963-problem-with-str_replace-array/#findComment-1122636 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.