Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Paul Cartier
October 7, 2009, 7:57 pm
rate this thread
I am trying to test if a URL contains either a V or P followed by any
sequence of numbers.
$topic = "Movies/A_League_of_Their_Own/V28669/";
echo "no match";
the above does not work, can anyone help? i hate reg expressions!
Remove the "X" from my email address
Paul Cartier (pcartierX@atlashosting.com) wrote:
: I am trying to test if a URL contains either a V or P followed by any
: sequence of numbers.
: $topic = "Movies/A_League_of_Their_Own/V28669/";
: if (preg_match("/^[PV][0-9][0-9]/",$topic_url))
: echo "match!";
: echo "no match";
: the above does not work, can anyone help? i hate reg expressions!
I can't test php on this computer, so I might be wrong. But,..
The regular expression you are looking for is the part that says
^ says start looking at the start of a line
[PV] says look for either P or V
[0-9] says look for a digit between 0 and 9
[0-9] says look for a second digit between 0 and 9
note the ^ that says look for the string at the _start of the line_.
If you looked for just [PV][0-9][0-9] then I think it would find the part
I marked with => <=.
Movies/A_League_of_Their_Own/ => V28 <= 669/
But the V28 is not at the start of a line, so if you add the ^ then there
is no match.
(Also note you probably need to be careful if you want to match the
slashes because they are part of the syntax of the expression, but I'm not
100% sure of the details of the slash in php, so I won't say exactly
On Oct 8, 1:32=A0am, "criman...@googlemail.com"
It's pretty sloppy to match the regex anywhere in the string. I'm
guessing you will always be looking at the last directory in the URL.
Since we don't know from the one example you gave what other values
are possible for $topic_url, it's hard to help you out too much. You
could help us out by posting some examples of real-life values that
If I were to assume that you can have any number of directories with
the one you want to check at the end, possibly followed by a trailing
slash, I would use this:
\/ - forward slash followed by
[PV] - a "P" or "V" followed by
\d+ - 1 or more digits followed by
\/ - a trailing slash
? - optionally, followed by
$ - end of string
This will match /my/path/to/V12345 and /other/path/to/P9876/ but not /
One suggestion for improvement, albeit a minor one. I would use a
different delimiter than a forward slash to avoid having to escape
it in the pattern. When it comes to writing regexes as clearly as
possible, I usually try to take every advantage (even small ones).
$re = '!/[PV]\d+/?$!';
Just my two cents, anyway.
<? $x='<? $x=%c%s%c;printf($x,39,$x,39);?>';printf($x,39,$x,39);?>