从php中的preg_match获取最后一个值

时间:2015-06-27 05:48:07

标签: php preg-match

我有一个字符串,其中包含一些网址,如何从字符串中获取最后一个网址

$myString = "::http://www.example.com/test/2015/06/27/mob/image_285245.jpg::http://www.example.com/test/2015/06/27/mob/image_288733.jpg::http://www.example.com/test/2015/06/27/285245";

        $pattern = "/http:\/\/(.*?)\/\d/";
        preg_match($pattern, $myString, $results);
        echo $results[1];  

//I want this:
//http://www.example.com/test/2015/06/27/285245

2 个答案:

答案 0 :(得分:4)

使用以下代码,

<?php 
    $myString = "::http://www.example.com/test/2015/06/27/mob/image_285245.jpg::http://www.example.com/test/2015/06/27/mob/image_288733.jpg::http://www.example.com/test/2015/06/27/285245";
    $results = explode("::", $myString);
    echo end($results);
?>

<强>输出:

http://www.example.com/test/2015/06/27/285245

答案 1 :(得分:0)

$myString = "::http://www.example.com/test/2015/06/27/mob/image_285245.jpg::http://www.example.com/test/2015/06/27/mob/image_288733.jpg::http://www.example.com/test/2015/06/27/285245";

$results = explode("::", $myString);

echo array_pop($results);

输出

http://www.example.com/test/2015/06/27/285245