regular expression for finding out 'anything <script>anything</script> anything' anywhere in string

64 Views Asked by At

i am trying to find out scrip tag both open() and close() in my php string.Please find my code below

public function custom_xss_clean($str){
  if(!preg_match('/(<\/?)(script|html)(>*)/i', $str)){
       return true;
  } else {
    $this->form_validation->set_message('custom_xss_clean','The %s field invalid');
    return false;
  }
}

This code is working fine in online tool but not in my code for string

<script>alert(10)</script>

I have tried so many pattern but no luck.Please help thanks in advance.

2

There are 2 best solutions below

0
On BEST ANSWER

Try This

preg_match('/<script>[a-zA-Z0-9 \. \n \t\r \* \\ \* ~`!@#$%^&*()-_+={}\[\]\'":;?\/><.,|]*<\/script>/', $str);

Am Tested also you can check live https://regex101.com/r/ih2C3F/1/

0
On

You mean you want to get everything between script tags?

<?php
    preg_match_all("|<[^>]+>(.*)</[^>]+>|U","<script>example: </script><div align=left>this is a test</div>",$out, PREG_PATTERN_ORDER);
    echo '<pre>',
    print_r($out);

if you are looking for xss_clean in codeigniter then follow this link click