Simple regular expression please!
Moderator: General Moderators
Simple regular expression please!
Dear All
How to I make a simple reg expression to return a number from a REQUEST_URI given of the form
http://<any_url_nonsense>/X
where X is a number, given at the end of the URL
Many thanks
Mark
How to I make a simple reg expression to return a number from a REQUEST_URI given of the form
http://<any_url_nonsense>/X
where X is a number, given at the end of the URL
Many thanks
Mark
Code: Select all
preg_match("#http://.*/(\d+)#", $url, $matches);
echo $matches[1];Hi weirdan,
It doesn't seem to be working! Maybe it is best that it simply ignores checking for a valid URL (it has to be, otherwise the script wouldn't be executed). It just needs to be a regex that will detect for /X at the end where X is a decimal.
Also, I need another regex that will detect for /pX where p is literally p, and X is again a decimal, again from an URL string.
They need to ignore for example for /X, it can't treat http://myurl.com/anything20 and pick up '20'. It has to be http://myurl.com/anything/20 otherwise it should return false. Similar case for /pX.
I would much appreciate any help.
Cheers
Mark
It doesn't seem to be working! Maybe it is best that it simply ignores checking for a valid URL (it has to be, otherwise the script wouldn't be executed). It just needs to be a regex that will detect for /X at the end where X is a decimal.
Also, I need another regex that will detect for /pX where p is literally p, and X is again a decimal, again from an URL string.
They need to ignore for example for /X, it can't treat http://myurl.com/anything20 and pick up '20'. It has to be http://myurl.com/anything/20 otherwise it should return false. Similar case for /pX.
I would much appreciate any help.
Cheers
Mark
Matching on http://<any_url_nonsense>/X is not the same as matching on http://<any_url_nonsense>/some-more-non-senseX
You should give a more accurate example of your requirements prior to asking for help. It is impossible to write a pattern matching expression when the pattern you have been supplied with is not correct. It not only wastes your time, but more importantly you are wasting the time of those trying to help.
Anyway....
You should give a more accurate example of your requirements prior to asking for help. It is impossible to write a pattern matching expression when the pattern you have been supplied with is not correct. It not only wastes your time, but more importantly you are wasting the time of those trying to help.
Anyway....
Code: Select all
$url = 'http://www.some-url.com/somedir/67';
if (preg_match('/\d+$/', $url, $match))
{
echo $match[0];
}redmonkey,
I'm not sure what you mean by wasting time - I haven't described the problem incorrectly as far as I'm aware. In any case, the following code, which is similar to that above, is not differentiating between /pX and /X, because if I put in /anything/pX, it regards it as both falling under the terms of /X and /pX. It successfully ignores however /anything/abcpX.
What is the problem with this code?
Thanks
Mark
I'm not sure what you mean by wasting time - I haven't described the problem incorrectly as far as I'm aware. In any case, the following code, which is similar to that above, is not differentiating between /pX and /X, because if I put in /anything/pX, it regards it as both falling under the terms of /X and /pX. It successfully ignores however /anything/abcpX.
Code: Select all
// Check for /pX, and if so return X into $out[2]
preg_match("/p(\d+)$/", $string, $out);
$is_p = !empty($out[1]);
if($is_p) {
echo 'Confirmed /pX: '.$out[1];
$pagecall = TRUE;
} else {
$pagecall = FALSE;
}
// Check for /X, and if so return X into $out2[2]
preg_match("/(\d+)$/", $string, $out2);
$is_X = !empty($out2[0]);
if($is_X) {
echo 'Confirmed /X: '.$out2[0];
$propcall = TRUE;
} else {
$propcall = FALSE;
}Thanks
Mark
redmonkey,
I've tried your regex form: it seems to work, however it cannot seem to tell the difference between /X and /pX (or indeed /anythinghereX). Also, when I rewrite it as
It doesn't seem to work for /pX, and only the /X form picks it up still.
Thanks
Mark
I've tried your regex form: it seems to work, however it cannot seem to tell the difference between /X and /pX (or indeed /anythinghereX). Also, when I rewrite it as
Code: Select all
/p\d+$/Thanks
Mark
Right, now I'm really confused. Here's the PHP code I'm using:
When I try the above regexs on the following strings using my copy of EditPro, I get the following results (I have had to remove the delimiting '/' from the ends of both regexs to use with EditPro). The expressions highlighted when using the regexs in EditPro have been displayed in italic:
For /p(\d+)$:
property.php/anything/p23
property.php/anythingp23
property.php/anything/23
property.php/anything23
For /(\d+)$:
property.php/anything/p23
property.php/anythingp23
property.php/anything/23
property.php/anything23
However, for the PHP code above, print_r simply outputs empty arrays.
Can anyone help me here?
Many thanks
Mark
Code: Select all
// Check for /pX, and if so return X into $out[2]
preg_match("/p(\d+)$/", $string, $out);
$is_p = !empty($out[2]);
if($is_p) {
echo 'Confirmed /pX: '.$out[2];
$pagecall = TRUE;
} else {
$pagecall = FALSE;
}
print_r($out);
// Check for /X, and if so return X into $out2[2]
preg_match("/(\d+)$/", $string, $out2);
$is_X = !empty($out2[1]);
if($is_X) {
echo 'Confirmed /X: '.$out2[1];
$propcall = TRUE;
} else {
$propcall = FALSE;
}
print_r($out2);For /p(\d+)$:
property.php/anything/p23
property.php/anythingp23
property.php/anything/23
property.php/anything23
For /(\d+)$:
property.php/anything/p23
property.php/anythingp23
property.php/anything/23
property.php/anything23
However, for the PHP code above, print_r simply outputs empty arrays.
Can anyone help me here?
Many thanks
Mark
Doh! I hadn't put down $string = $_SERVER['REQUEST_URI'];
Okay, for the string
It still detects it incorrectly as /X and does not detect it as /pX. However, in EditPro this same set of regexs does. In other words, it could be /property.php/anything9055 and the two regexs above would still incorrectly detect it as /X and give X as 9055. Also, obviously from above /pX doesn't seem to be working as it doesn't say it has detected it as /pX, where obviously it should.
Is my computer jinxed or something? Anything obvious?
Many thanks
Mark
Okay, for the string
Code: Select all
/property.php/p9055Is my computer jinxed or something? Anything obvious?
Many thanks
Mark
By waisting time I meant that by asking to match one type of pattern when you actually need to match another type, it is a waste of time. Weirdan's regex will work for your original request.
I'm still not entirely sure what you are trying to achieve but the following may help...
This will match and extract the required info for...
$url = 'http://www.some-url.com/somedir/67';
and..
$url = 'http://www.some-url.com/somedir/p67';
I'm still not entirely sure what you are trying to achieve but the following may help...
Code: Select all
$url = 'http://www.some-url.com/somedir/p67';
$pagecall = false;
$propcall = false;
if (preg_match('/(p??)(\d+)$/', $url, $match))
{
if (!empty($match[1]))
{
$urlbit = $match[1] . $match[2];
$pagecall = true;
}
else
{
$urlbit = $match[2];
$propcall = true;
}
echo $pagecall ? 'Confirmed /pX : ' . $urlbit : '';
echo $propcall ? 'Confirmed /X : ' . $urlbit : '';
}$url = 'http://www.some-url.com/somedir/67';
and..
$url = 'http://www.some-url.com/somedir/p67';
Hi redmonkey,
I've copied your code exactly, and it's still not working property (but thanks for making the effort). If I put into my browser bar (to return to $REQUEST_URI) the following address form
http://<mydomain.com>/property.php/p235
for example, it returns p235 but correctly detects it of the form /pX. I need it to return just 235 though, without the 'p'.
If I put in
http://<mydomain.com>/property.php/anything235
it returns that it detected it as /X and returns 235. However it should only return as /X if it was
http://<mydomain.com>/property.php/anything/235
(and obviously return 235).
I hope that makes things a little clearer - if you know how to ammend the code I would be very grateful
Mark
I've copied your code exactly, and it's still not working property (but thanks for making the effort). If I put into my browser bar (to return to $REQUEST_URI) the following address form
http://<mydomain.com>/property.php/p235
for example, it returns p235 but correctly detects it of the form /pX. I need it to return just 235 though, without the 'p'.
If I put in
http://<mydomain.com>/property.php/anything235
it returns that it detected it as /X and returns 235. However it should only return as /X if it was
http://<mydomain.com>/property.php/anything/235
(and obviously return 235).
I hope that makes things a little clearer - if you know how to ammend the code I would be very grateful
Mark
Code: Select all
if (preg_match('/\/(p??)(\d+)$/', $url, $match))
{
if (!empty($match[1]))
{
$pagecall = true;
}
else
{
$propcall = true;
}
echo $pagecall ? 'Confirmed /pX : ' . $match[2] : '';
echo $propcall ? 'Confirmed /X : ' . $match[2] : '';
}Code: Select all
if (preg_match('/\/(p??)(\d+)$/', $url, $match))
{
$urlbit = $match[2];
}
echo isset($urlbit) ? 'Page requested : ' . $urlbit : 'No page selection';