[thelist] Please help.. PHP guru's

Juha Suni juha.suni at ilmiantajat.fi
Thu Feb 24 08:01:32 CST 2005


Tim Burgan wrote:
> I have a string:
> $str = '<p>This is a paragraph<p><?php echo \'hello\';
> ?>';

snip

> But.. I need to manipulate the string in such a way that the PHP tags
> to STAY ENCODED like:
> <p>This is a paragraph</p>&lt;?php echo 'hello'; ?&gt;

Well I bet there are a thousand ways to do this with regexp. For
quick'n'dirty I would do
something like:

$str = '&lt;p&gt;This is a paragraph&lt;p&gt;&lt;?php echo \'hello\';
?&gt;';
$str = str_replace("&lt;?php","XXPHPSTARTAGXX",$str);
$str = str_replace("?&gt;","XXPHPENDTAGXX",$str);
$str = html_entity_decode(stripslashes($str));
$str = str_replace("XXPHPSTARTAGXX","&lt;?php",$str);
$str = str_replace("XXPHPENDTAGXX","?&gt;",$str);

The real question, however is what is it that you are really trying to do,
in a larger context? Your starting point already seems a bit strange, having
entity-encoded html and php in the same string, especially since you need to
process them in different ways. I'd say your real problem is somewhere
before getting to this point.

The above code should, however, help.

HTH

-- 
Suni



More information about the thelist mailing list