简体   繁体   中英

using regex in php to check string and split if matched

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

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];
}
$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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM