1

I am using following regular expression to match the url paths

<?php
  $path1 = 'user/3/categories';
  $path1 = 'user/3';
  $re = /^user\/[0-9]+\/?$/;
  $match =  preg_match($re, $path1);
  echo $match;
  $match =  preg_match($re, $path2);
  echo $match;

My expected answer should print 0 and 1, However I am getting error in regular expresion in line 3. What is error I am making ?

2
  • 4
    Unlike javascript in php regex patterns are strings. use $re = '/^user\/[0-9]+\/?$/';. Here's solution and how it was fixed. so you can realize mistakes in a case you're new to php.
    – Leri
    May 23, 2013 at 11:26
  • 1
    You can also use another delimiter to simplify the pattern: '~^user/[0-9]+/?$~';
    – meze
    May 23, 2013 at 11:29

1 Answer 1

4

You forgot the quotes

$re = '/^user\/[0-9]+\/?$/';

1
  • wrapping inside quotes fixed problem. Learned that in php regex are not a data type in itself. Thanks everyone May 26, 2013 at 3:49

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.