Forum Moderators: coopster

Message Too Old, No Replies

how to search/replace within. a function?

php'd into a corner

         

Josefu

7:34 pm on Feb 4, 2007 (gmt 0)

10+ Year Member



I must say that this one has me flummoxed since days.

I had the enrmous task around a year ago of transforming an 8.000 + page HTML site into dynamic php, in incorporating a new look in the process. To do this, all in keeping the original file heirarchy (sitemap), I stripped everything "structure" from the above files to leave nothing but the content, and this I made into a function:

<?php
function content() {
?>

<p>All the original HTML here </p>
<p>All the original HTML here </p>
<p>All the original HTML here </p>

<?php
}
?>

...then at the bottom of the page I would call the template (that had headers, columns, variables and such) that itself would call the 'content()' function above in the right place.

I use this technique for new content as well. Yet recently I got the offer to put a block text into the content - I could do this by hand, but have devised a 'magic_word' sytem that would search the page for that 'magic_word' (should I put it in) and replace it with the proper ad. The problem is, I can't seem to get any search and replace to work within the above - even after I pass the completed function to a variable! It just prints out, no matter what I do.

Has anyone ever seen anything like this before? I seem to have php'd myself into a corner.

ericjust

7:48 pm on Feb 4, 2007 (gmt 0)

10+ Year Member



<?php
function content() {
?>

This content will always be outputted because it isn't within PHP tags anymore. You must use output buffering to capture this into a string so that you can do a search on it. See [us3.php.net...]

<?php
}
?>

Josefu

9:21 pm on Feb 4, 2007 (gmt 0)

10+ Year Member



That was it exactly sir - thanks a million! Never would have been able to find that one myself.

I really should get all of the content into a database to avoid that problem though - as the above probably creates a lot of server overhead.

Thanks again, and cheers,

ThePromenader.

whoisgregg

3:42 pm on Feb 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you could also use heredoc [us3.php.net] syntax. (untested)

<?php 
function content() {
return <<<___EOF
?>
<p>All the original HTML here </p>
<p>All the original HTML here </p>
<p>All the original HTML here </p>
<?php
___EOF;
}
echo str_replace('p>', 'li>', content());
?>

Josefu

4:36 pm on Feb 6, 2007 (gmt 0)

10+ Year Member



...that would almost be it - but it doesn't seem to be working on my hosting server. If I were able to use the heredoc syntax, I would not need to create a function - the HTML code could be compiled into a variable.

<?php

$content = <<<HTML_HERE

<p>All the original HTML here </p>
<p>All the original HTML here </p>
<p>All the original HTML here </p>

HTML_HERE;

include ({template}};

?>

...but that would about amount to the same. I haven't a clue why the heredoc is not working though.

ericjust

4:53 pm on Feb 6, 2007 (gmt 0)

10+ Year Member



This works fine for me. Hmmm....


<?php

$content = <<<HTML_HERE

<p>All the original HTML here </p>
<p>All the original HTML here </p>
<p>All the original HTML here </p>

HTML_HERE;

$content = strtolower($content);
echo $content;

?>