-1

I'm back again to show my ignorance once more.

I need assistance grabbing numbers that are in a URL in a specific spot. The rest of the URL may also contain numbers so I need to restrict it to grabbing the numbers from just one location, for example:

http://www.example.com/99-Kittens-1382/animals

I had been using ereg_replace to grab all the numbers but then realized:
1) ereg_replace is deprecated
2) Some URLs may have numbers in them elsewhere.

So in this case I ended up with 991382 instead of just the 1382 that I wanted.

The URL structure should always be that we have -####/ so I think we should be able to match based off of that. (dash, four numbers, forward slash)

I've always been terrible with regular expression matching. Can anyone help me out?

Scott Rowley
  • 486
  • 1
  • 7
  • 30
  • 1
    See [converting ereg to preg (missing regex delimiters)](http://stackoverflow.com/questions/6270004/converting-ereg-expressions-to-preg) for the deprecated issue and [open source regexbuddy](http://stackoverflow.com/questions/89718/is-there-anything-like-regexbuddy-in-the-open-source-world) or [online regex testing](http://stackoverflow.com/questions/32282/regex-testing-tools) for some helpful tools. – mario Jan 16 '12 at 17:39

4 Answers4

2

This is probably better done without the complication of regex:

$urlParts = parse_url($str);
$pathParts = explode('/', $urlParts['path']);
$firstParts = explode('-', $pathParts[1]);
$numberYouWant = array_pop($firstParts);

See it working

DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • perfect. Thank you. I'll 'check' your answer in 5 minutes when it allows me to. – Scott Rowley Jan 16 '12 at 17:45
  • Have you tried the performance of this? In my experience explode() is quite expensive, maybe more so than a regex. parse_url seems also likely to be quite intensive. – Eugen Rieck Jan 16 '12 at 17:50
  • I implemented it on the page I needed (in a header) and the page loads in under 1 second. – Scott Rowley Jan 16 '12 at 17:55
  • @EugenRieck I have not, although we are probably talking microseconds - it depends how many times you need to do this in a single execution of the script. Bare in mind that the PCRE engine is quite expensive as well... – DaveRandom Jan 16 '12 at 17:56
1
preg_match('#-(\d{4})/#', $url, $matches);
echo $matches[1];
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
Francis Avila
  • 31,233
  • 6
  • 58
  • 96
  • Not bad, but what if the url is `http://www.example.com/I-Would-Like-2199-Kittens-1382/animals` - you will get `2199`. – DaveRandom Jan 16 '12 at 17:42
  • Still works. You won't match 2199 because of the `/` requirement. The only assumptions this regex makes are 1) required number is 4 digits, 2) is bordered by `-` and `/`, 3) the first match is the only one that matters. Conceivably we could have `http://www.example.com/something/kitten-1234/animals`, but I don't know enough from the OP whether matching is correct in this case. We would need a full range of inputs and exactly the part that is desired (e.g., "final digits after a dash in the first url path part".) – Francis Avila Jan 16 '12 at 17:49
  • Fair point, forgot about the `/`. The only real place where this fals down on the OPs requirements (I suspect) is that the number of digits may not always be 4. Although this could easily be circumvented with a `{1,n}` clause where `n` is the max number of chars. – DaveRandom Jan 16 '12 at 17:55
1
$a="http://www.example.com/99-Kittens-1382/animals";
echo preg_match("/.*?\\/(\\d+)-.*?-(\\d+)\\//",$a,$m)."\n";
print_r($m);

gives

1
Array
(
    [0] => http://www.example.com/99-Kittens-1382/
    [1] => 99
    [2] => 1382
)
Eugen Rieck
  • 64,175
  • 10
  • 70
  • 92
0
$return = preg_match('/-([0-9]{4})/', 'http://www.example.com/99-Kittens-1382/animals', $matches)

Should work, the ( and ) group the match, [0-9] is the range of characters to match and {4} is the exact amount of the preceding group you want to match. The match is in $matches[1]

Adam Field
  • 101
  • 1
  • 2