Since I dont want to use stip_tags function of php instead of I want to replace <script> and </script> to empty string so that output should be alert(1).
Input:-
<script>alert(1)</script>Output:- alert(1)
how to achieve it.
Either use a simple replace:
$string = str_replace(array("<script>","</script>"), "");
or a RegEx:
$string = preg_replace("/<script.*?>(.*)?<\/script>/im","$1",$string);
Easy way is using StripTags.
go to StripTags github and download or install via composer.
this class has a method called only that do this for you!
use Mimrahe\StripTags\Stripper;
$stripper = new Stripper();
$stripper->text('<a href="#">link</a><p>some paragraph</a>');
$stripper->only(['p']);
echo $stripper->strip(); // prints '<a href="#">link</a>some paragraph'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With