Jump to content

What's wrong with this STR_REPLACE ?


roughie

Recommended Posts

One of the two strings being replaced, "takes",

whereas the other doesn't! But WHY?

 

file1: test.php

 

<?php

$yyy = "abcde";

$zzz = "hijkl";

$tmp = @file_get_contents("test.tpl");

$tmp = str_replace(array("%%xxx%%%", "%%www%%"), array($yyy, $zzz), $tmp);

echo $tmp;

?>

 

file1: test.tpl

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head><title>tst replace</title></head>

<body>

<div>

  <label> Value 1: </label>

<input type="text" name="listid" size="10" value="%%xxx%%" />

  <br />

  <label> Value 2: </label>

<input type="text" name="listid" size="10" value="%%www%%" />

</div>

</body>

</html>

 

 

Link to comment
https://forums.phpfreaks.com/topic/91905-whats-wrong-with-this-str_replace/
Share on other sites

<?php

$string = <<< EOD
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head><title>tst replace</title></head>
<body>
<div>
     <label> Value 1: </label>
   <input type="text" name="listid" size="10" value="%%xxx%%" />
     

     <label> Value 2: </label>
   <input type="text" name="listid" size="10" value="%%www%%" />
</div>
</body>
</html>
EOD;

$yyy = "abcde";
$zzz = "hijkl";
$tmp = str_replace(array("%%xxx%%", "%%www%%"), array($yyy, $zzz), $string);
echo $tmp;

?>

 

You have an extra percentage sign on the "%%xxx%%"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.