Re: preg_replace Guru question

From: Date: Thu, 27 Nov 2003 23:14:39 +0000
Subject: Re: preg_replace Guru question
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-171234@lists.php.net to get a copy of this message
On Friday, November 28, 2003, at 12:17 AM, pete M wrote:
What Preg_replace code would I need to highlight all the code within the "<script </script>" tags in red.. (case insensitive) What I want is a view_source.php page to highlight the javascript for a tutorial I'm writing. Can view the source ok but need to highlight the code. Not a regular expression expert ;-(
UNTESTED: <? $old = "before before <script type='text/javascript'> function foo() { // bah } </script> after after"; // i assume the source will have htmlentities applied... $old = htmlentities($old); // ...therefore we need to match on &lt; and &gt; rather than < and > $new = preg_replace('/&lt;script(.*?)&gt;(.*?)&lt;\/script&gt;/s','&lt;script\\1&gt;<span style="color:red;">\\2</span>&lt;/script&gt;',$old); echo "<pre>{$new}</pre>"; ?> season to taste :) Justin French

« previous php.general (#171234) next »