note 37907 added to function.ereg-replace
| From: | ground-pilot at net-pilots dot com | Date: | Tue, 02 Dec 2003 01:45:49 +0000 |
| Subject: | note 37907 added to function.ereg-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-61239@lists.php.net to get a copy of this message | ||
This should fix content manager issues regarding embeded javascript
code being non-discriminently changed to contain extra "<br>\n\r"
tags after using the nl2br() PHP function call in the your string
containing your page content.
For some this isn't an issue. For me it is. And power-users have
asked why they can't embed JavaScript in their pages using my
system. Well, here's my solution that I have in my content as the
content from SQL is taken and sent to the visitor. It does not
require any changes to your web based content manager administration
code (where your site owners update their page content) or in your
SQL based content sources - as it does this handy switcheroo on
the way out the door to the visitor/client.
<?
/*
* This little routine will find all contents between
* <script... and </script> tags. This should only be
* JS right? Okay, so errors can arise, but this is close.
*
* CAVEAT: There may be more than one instance of the
* following on the page :
*
* <script LANGUAGE="JavaScript">
* <!-- Hide the Script from non-JavaScript browsers --
* ...
* // -- End hiding the script from non-JavaScript browsers -->
* </script>
*
* You should iterrate through each $matches and adjust
* the contents accordingly, this script accomplishes
* that task for you by looping through each $matches[]...
*/
$content = nl2br($res[content]);
$preg = "/(<script[^>]*>)(.*)(<\/script>)/sU";
preg_match_all($preg, $str, $matches);
for ($i=0; $i< count($matches[0]); $i++) {
$reg = $matches[0][$i];
$js_out = ereg_replace("<br>", "\n", $reg);
$js_out = ereg_replace("<br />", "\n", $reg);
$content= str_replace($reg, $js_out, $content);
}
echo $content;
?>
Yeah - there's plenty of ways to improve or make it a tidy little
function call. But this works for the two insances I need it, even
though it's not without flaws of it's own. However nl2br() should
also be a little more <script> aware when adjusting html contents
inside a string too? :)
--- ENJOY!
----
Manual Page -- http://www.php.net/manual/en/function.ereg-replace.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+37907
Delete -- http://master.php.net/manage/user-notes.php?action=delete+37907&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+37907&report=yes
Search -- http://master.php.net/manage/user-notes.php