Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ereg_replace() question...

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Hi. Can someone please tell me why;

$filename = "template.html";
$content = ReadFile($filename);

$show = "something here to see if works ok";
$content = ereg_replace("::CONTENT::", $show, $content);
echo $content;

doesnt replace the ::CONTENT:: part in the template.html file (saved as the $content string).

Anyone got any ideas? I have tried escaping the :'s, but that doesnt work.

Thanks

Andy
 
From:
The following script works for what you want. readfile() is only redirecting the content of the file to the browser. So even if you did successfully change the variable content it would not get displayed. See the script at the end for an example. readfile has a prototype of int not string. It sends the file content to stdout and then returns the byte count of the file to your variable. Your output would look like this:

::CONTENT::80

<?
$fp = fopen(&quot;template.html&quot;,&quot;r&quot;);
$content = fread($fp,10000);
fclose($fp);

$show = &quot;something here to see if works ok&quot;;
$test= str_replace(&quot;::CONTENT::&quot;, $show , $content);
echo $test;
?>
 
I tried this to see if it was the format, and that didnt work either :(

<?
$template = &quot;hello testing now&quot;;

$word = &quot;testing new&quot;;

$edit = &quot;testing&quot;;

str_replace(&quot;$edit&quot;,&quot;$word&quot;,&quot;$template&quot;);
// word to|word to | thing to
// look for|replace | search for
// with | it in

echo $template;

?>

Thanks

Andy
 
You almost got it. str_replace and ereg-replace do not alter the original string. For this to work send the results of the replace to a new variable:

$altered_template=reg_replace &quot;$edit&quot;,&quot;$word&quot;,&quot;$template&quot;);

or

$altered_template=str_replace(&quot;$edit&quot;,&quot;$word&quot;,&quot;$template&quot;);

========================================
Here are two ways complete


<?
$template = &quot;hello testing now&quot;;

$word = &quot;testing new&quot;;

$edit = &quot;testing&quot;;

$altered_template=ereg_replace(&quot;$edit&quot;,&quot;$word&quot;,&quot;$template&quot;);

echo $altered_template;

?>

and this works too:


<?
$template = &quot;hello testing now&quot;;

$word = &quot;testing new&quot;;

$edit = &quot;testing&quot;;

$altered_template=str_replac(&quot;$edit&quot;,&quot;$word&quot;,&quot;$template&quot;);

echo $altered_template;

?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top