Jump to content

[SOLVED] how to use preg_replace()


andz

Recommended Posts

@MadTechie:

 

Thanks for the solution mate.

 

I used this solution of yours ::

 

$content = preg_replace('%<script.*?>.*?</script>%sim', '', $content );

 

how about if i have multiple tags on the $content, example

 

$content = 'mymessage<script>alert("test")</script><embed>source</embed>';

 

how do i add that to validation?

use htmlspecialchars

ie

$content = htmlspecialchars($content, ENT_QUOTES);

 

or even try this

$content = 'my <b>message</b><script>alert("test")</script> <embed>source</embed>';
$content = preg_replace('%<([^>]*)>(.*?)</\1>%sim', '\2', $content);
$content = htmlspecialchars($content, ENT_QUOTES);
echo $content;

i'd like to but to the script that I'm working, applying htmlspecialchars() is not an options as some of the scripts will not function.

 

i already tried applying it, although it worked but some pages suffered.

 

i wanted to validate the <script></script> and <embed></embed> tags

 

how do i put it into action considering this example below?

 

 

$content = 'mymessage<script>alert("test")</script><embed>source</embed>';

 

 

thank you very much

ok this removes embed and script

//simple
$content = preg_replace('%<(script|embed)[^>]*>(.*?)</\1>%s', '\2', $content );

//try this
$remove = array('script','embed');
$remove = implode("|", $remove);
$content = preg_replace('%<('.$remove.')[^>]*>(.*?)</\1>%s', '\2', $content );

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.