1

Users at My website Post Content in WYSIWYG, so they can also add harmful java script and Style sheet. so just to detect java script tag in my content I've written this regex->

$regex = "/\<script(.*?)?\>(.|\\n)*?\<\/script\>/i";

preg_match_all($regex, $html, $scripts);

print_r($scripts);

regex such that print_r($scripts) will give me:

array(
 [0] => <script src="http://example.com"></script>
 [1] => <script>// inline js$(document).ready( function() {});</script>
 )

How can i do the same with stylesheet tag and remove javascript tag and stylesheet tag. above code only detect javascript how can i remove this tag

10
  • If you replace 'script' with 'style' in your regex, you should get the style tags. Commented Apr 10, 2016 at 8:06
  • ok thanks, but how can i remove these tags? Commented Apr 10, 2016 at 8:07
  • Instead of match, use replace. Commented Apr 10, 2016 at 8:08
  • 1
    preg_replace("/<script(.*?)?\>(.|\\n)*?\<\/script\>/", "", $input_lines); and preg_replace("/<style(.*?)?\>(.|\\n)*?\<\/style\>/", "", $input_lines); Commented Apr 10, 2016 at 8:09
  • @rock321987 for link rel="stylesheet" Commented Apr 10, 2016 at 8:12

2 Answers 2

1

For removing tags, you can use preg_replace as

preg_replace("/<.*script.*>(.|\\n)*<\/script>/", "", $input_lines);

preg_replace("/<.*stylesheet.*>(.|\\n)*<\/stylesheet>/", "", $input_lines);

There is no need to escape < and > and you can use .* instead of (.*?)?. Also I am making it greedy for probable nested tags by using (.|\\n)* instead of (.|\\n)*?

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried this?

$storeHere = strip_tags(whatever_you_want_to_strip_tags_from);

1 Comment

if i strip the tags then all <p><br><img> tag will be also removed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.