在php中使用正则表达式检查字符串并在匹配时拆分
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
我需要检查数千个URL并将其分解为单独的变量。 p> \ n
我正在寻找的网址如下: - p>
http://www.mydomain.com/facebook-pages/252090964881271-smirnoff p>
http://www.mydomain.com/facebook-pages/3312880235580-facebook-for-every-phone p>
http:// www。 mydomain.com/facebook-pages/2449443856-candy-crush-saga p>
我想忽略的其他人没有像这样的数字: - p> \ n
http://www.mydomain.com/facebook-pages/media/ p>
我需要一个可以识别开始的字符串的正则表达式模式 使用: - p>
http://www.mydomain.com/ facebook-pages / and还包含一系列数字(可变长度)。 p>
然后我需要提取代码(数字)和数字后的文本 ,例如: - p>
$ pagecode =“2449443856 p>
$ pagename =”candy crush saga“ p>
从这个字符串 http://www.mydomain.com/facebook-pages / 2449443856-candy-crush-saga p>
我尝试了所有不同的正则表达式字符串,我真的很挣扎。 任何人都可以帮忙吗? p>
谢谢 p>
Jonathan p> div>
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];
}