[thelist] PHP - str_replace problem

Hassan Schroeder hassan at webtuitive.com
Wed Oct 20 01:20:44 CDT 2004

Chris Ditty wrote:
> Hi all.  I'm trying to do a little code snippets page for a site I am
> working on.  I figured it would be simple enough.  I would do a
> str_replace and replace the various html codes with the ascii
> eqivulant. 

> $snippetCode = str_replace("\n", "<br>", $snippet['snippetCode']);
> $snippetCode = str_replace("<", "&gt;", $snippetCode);
> $snippetCode = str_replace(">", "&lt;", $snippetCode);
> $snippetCode = str_replace("&", "&amp;", $snippetCode);
> This is what is in $snippet['snippetCode'].
> ?><pre><? print_r($ArrayName); ?></pre><?
> This is what is showing on the web page.
> ?&lt;&gt;pre&lt;&gt;? print_r($ArrayName); ?&lt;&gt;/pre&lt;&gt;?

Uh, isn't that what you've told it to create?  :-)

'<' gets turned into '&lt;' which is then turned into '&amp;lt;' *

Might want to consider the order of those replacement statements!

(*Note: < is actually lt and > is gt)

Hassan Schroeder ----------------------------- hassan at webtuitive.com
Webtuitive Design ===  (+1) 408-938-0567   === http://webtuitive.com

                           dream.  code.

More information about the thelist mailing list