0

i want to check the script tag in an url

For example: if url have the script tag the below senario is working fine..

<?php
 $url = 'admin/content/test/<script>';
 preg_match('<script>', $url, $match);
 if (count($match) >= 1) {
  print 'It executes';
 }
?>

output: It executes

but in my case if the url have a string "product_description" then also above condition matches

<?php
  $url = 'admin/content/product_description/test';
  preg_match('<script>', $url, $match);
  if (count($match) >= 1) {
   print 'It executes';
  }
?>

output: It executes

Please suggest the right way to check the script tag in an url..

2 Answers 2

2

Try using strpos instead of preg_match:

if (strpos($url,'<script>') !== false) {
   print 'It executes';
}

Here you have the manual of this function: documentation

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

2 Comments

You are welcome. preg_match is sensible to special characters, when strpos isn't.
this does not work with <script type="text/javascript">
0

You have to put the regex inside delimiters:

preg_match('/<script>/', $url, $match);

You version worked because the pair <> were considered as delimiters, it was the same as:

preg_match('/script/', $url, $match);

and it matched script and also description

Comments

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.