使用preg_match_all返回Wordpress中图像的部分网址

时间:2014-09-16 18:57:26

标签: php regex wordpress url preg-match-all

我试图修改下面的功能,所以它会返回帖子第一个图片网址,而不是http://,域名网址和结尾的反斜杠,但我真的不知道如何在这种情况下处理正则表达式。顺便说一下,我在Wordpress工作。

这是函数返回的内容:
http://www.domain.com/wp-content/uploads/2014/09/image.jpg

这就是我需要的:
wp-content/uploads/2014/09/image.jpg

function catch_first_image()
{
    global $post, $posts;
    $first_img = '';
    ob_start();
    ob_end_clean();
    $output = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches );
    $first_img = $matches[1][0];

    echo $first_img;
}

对正确的正则表达式的进一步解释将非常感谢:) 提前谢谢!

3 个答案:

答案 0 :(得分:2)

<强> Live demo

$output = preg_match_all( "/<img.*https?:\/\/[^\/]+\/([^\"']+)[\"'].*/i", $post->post_content, $matches );

echo $matches[1][0];

说明:

.* => any character zero or more times
https? => http or https
[^\/]+ => any character other than / at least one time
[\"'] => double or single quote

答案 1 :(得分:0)

使用此表达式:

~<img.+?src=['"]http://www\.domain\.com/([^'"]+)['"].*?>~i

Demo

我只是在您引用的第一个捕获组之外的http://www.domain.com字面上匹配。请注意,我将分隔符更改为~,因此我们不需要转义斜杠。您可能希望将其更改为https?://(?:www\.)?以使事情更加灵活。不要忘记逃避时期。此外,我让你的点匹配 - 所有重复都很懒,以免你在将来头痛(.+?.*?)。

答案 2 :(得分:0)

<img.+?https?://[^\/]+\/\K[^\"']+

Demo