0

I have a thousands of URL's that I need to check and explode into separate variables.

The url's I'm searching for look like this:-

http://www.mydomain.com/facebook-pages/252090964881271-smirnoff

http://www.mydomain.com/facebook-pages/3312880235580-facebook-for-every-phone

http://www.mydomain.com/facebook-pages/2449443856-candy-crush-saga

Others I want to ignore do not have numbers in them like this:-

http://www.mydomain.com/facebook-pages/media/

I need a regex pattern that can identify strings that begin with:-

http://www.mydomain.com/facebook-pages/ and ALSO contain a series of numbers (of variable length).

I then need to extract the code (the numbers) and the text after the numbers, for example:-

$pagecode = "2449443856

$pagename = "candy crush saga"

from this string http://www.mydomain.com/facebook-pages/2449443856-candy-crush-saga

I have tried all different regex strings and I'm really struggling. Can anyone help please?

Thanks

Jonathan

1
  • Which of the regex strings you tried was the closest to what you need? Commented Jun 17, 2013 at 15:31

2 Answers 2

1

Try this,

$str = 'http://www.mydomain.com/facebook-pages/2449443856-candy-crush-saga';
if (preg_match('/^http:\/\/www.mydomain.com\/facebook-pages\/([0-9]+)-(.*?)$/si', $str, $match)) {
  $page_no = $match[1];
  $page_name = $match[2];
}
Sign up to request clarification or add additional context in comments.

1 Comment

awesome thanks, I had to tweak it slightly but you truly helped me. Thanks so much!
0
$string = 'http://www.mydomain.com/facebook-pages/2449443856-candy-crush-saga';

if (preg_match('@^http://www.mydomain.com/facebook-pages/([0-9]+)-(.+)$@', $string, $matches)) {
    $pagecode = $matches[1];
    $pagename = $matches[2];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.