Drew Baker
Drew Baker

Reputation: 14430

PHP RegEx string contains more than two numbers next to each other enclosed with slashes

My RegEx is poor, and I have been struggling with this. I have some URL's that contain a pattern like this:

/1234/
/5527191/
/15974/

And so on. It's always a bunch of letters, then a slash, numbers ( no more than 10 numbers) and then another slash.

So I'm after some RegEx that will search, ignore letters and find a group of numbers bewteen two slashes.

Thanks for the help!

Upvotes: 1

Views: 1038

Answers (1)

Mark Byers
Mark Byers

Reputation: 838706

You can use preg_match_all (or preg_match if you only want the first match) with the following regular expression:

preg_match_all("#/\d{1,10}/#", $s, $matches);

Explanation

  • # is the delimiter for the regular expression.
  • / matches a literal slash.
  • \d matches any digit.
  • {1,10} matches the previous token between 1 and 10 times.

See it working online: ideone

If you want to also capture the number without the slashes you could use a capturing group:

preg_match_all("#/(\d{1,10})/#", $s, $matches);

Upvotes: 1

Related Questions