Page 1 of 1

Regex for this?

Posted: Fri Aug 06, 2004 3:42 pm
by mjseaden
Dear All,

I have an URL address of the form

/string/anotherstring/yetanotherstring21

Any number of /strings/ can occur in the address - can anyone tell me if I can

[a] Detect whether or not a number has occurred after 'yetanotherstring'

and if so

Extract this number into a variable

Sometimes, a number will not appear at the end and therefore this case needs to be discounted.

Many thanks

Mark

Posted: Fri Aug 06, 2004 3:46 pm
by feyd
untested

Code: Select all

preg_match_all('#/\w*((\d+)\w*)*$#',$url,$matches)

Posted: Fri Aug 06, 2004 3:49 pm
by Weirdan
seems like it should work... just add the literal slash after the first asterisk.

Posted: Mon Aug 16, 2004 11:24 am
by mjseaden
Hi feyd/weirdan - thanks for the response. It doesn't seem to be working I'm afraid. I'm using the following PHP code:

Code: Select all

// Check if a property information page is called via /<type>X
if( preg_match('#/\w*((\d+)\w*)*$#',$url,$match2) && $pagecall != true )
{
    if(!empty($match2[1]))
    {
        $propcall = true;
        echo 'Property Info: '.$match2[2];
    }
}

Posted: Mon Aug 16, 2004 11:26 am
by mjseaden
It doesn't seem to detect the URL properly. If I add a ''' before the first *, it reports an unmatched parentheses error.

Posted: Mon Aug 16, 2004 3:21 pm
by mjseaden
Hi there - can anyone help?

Many thanks

Mark

Posted: Mon Aug 16, 2004 4:12 pm
by markl999
I'd just do:

Code: Select all

preg_match('#[0-9]*$#', basename($url), $match);
if(!empty($match[0])){
  echo $match[0]; //it did have a number on the end
} else {
  echo 'no ending numbers';
}