php使用preg_match在href标签内获取2个字符串

时间:2014-08-26 12:07:27

标签: php preg-match

我需要一个代码来从href标签中获取这些字符串

weburl的例子:

/video/funny-videos-with-dogs-21608674/

我需要的字符串是:

网址标题=搞笑视频与狗

网址ID = 21608674

我如何通过preg_match从网址获取这2个字符串?

更新

到目前为止我尝试的是:

preg_match('/\/video\/(.*?)-/is', $vUrl, $vUrl_Title);

但显示我只是“有趣”...我需要一些东西可以显示“搞笑视频与狗”

1 个答案:

答案 0 :(得分:1)

这应该很简单:

<?php

if (preg_match("#/video/([a-z\-]+)-([0-9]+)/#", "/video/funny-videos-with-dogs-21608674/", $matches)) {
  print_r($matches);
  $urlTitle = $matches[1];
  $urlID    = $matches[2];
}
else {
  print_r("Not found!");
}

然后大叫

Array
(
    [0] => /video/funny-videos-with-dogs-21608674/
    [1] => funny-videos-with-dogs
    [2] => 21608674
)

考虑到非其他字符应该匹配。我很确定你可以简化正则表达式,我没有深刻的正则表达式知识,但这应该有效