Why this php code not work when tried to remove script tag? -


normally use code remove data inner img tag. it's work good.

<?php $string = "<b>test</b><img src=\"https://www.google.co.th/images/nav_logo242.png\"><script>alert();</script>"; $string = preg_replace("/<img[^>]+>/", "", $string); echo $string; ?> 

then apply code remove data inner script tag. code.

<?php $string = "<b>test</b><img src=\"https://www.google.co.th/images/nav_logo242.png\"><script>alert();</script>"; $string = preg_replace("/<scrip[^>]+script>/", "", $string); echo $string; ?> 

when test code it's not remove data inner script tag. why ?

your code doesn't work because you're parsing <scrip, followed 0 or more characters other >, followed script>.

there no such substring in content. in $string, after <scrip have t (which matches [^>]+) , have > instead of script>. so, no match.

here's need instead:

$string = preg_replace("/<script.*?<\/script>/si", "", $string); 

you cannot use [^<] or [^>] because javascript code may contain many < , > characters itself.

here's above regex does:

• search <script
intentionally did not include closing > bracket here, because maybe have attributed in script tag, <script type='text/javascript'>

• followed sequence of random characters, using lazy evaluation
note .*? instead of .*, captures little characters possible find match, instead of as possible. avoids following problem:
<script>something</script> other content <script>more script</script>
without lazy evaluation, remove everything first <script> last </script>

• followed </script> mark end of script section
note i'm escaping slash (\/ instead of /) because / regex delimiter character here. have used different character @ beginnen , end of regex, #, , / didn't have escaped.

• finally, added s , i modifiers. s make parse multiline content. javascript code can of course contain linebreaks, , want .*? match well. , i make case insensitive, because assume want replace <script> or <script> too.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -