从一个位置切换到另一个位置

时间:2013-11-30 11:39:22

标签: php string split

首先:我有以下字符串内容

Autumn, tree leafs<BR/><BR/><a title='Autumn, tree' href='http://site.net/share-picture/3K'><img width='225px' src='http://site.net/_s/upload/2013/11/29/0be99469a67aaf35b30b236e8ee9faa3.jpg225.jpg' title='Autumn, tree leafs' alt='Autumn, tree leafs'/></a><br/>

现在,我想要的是剪切该字符串的一部分,特别是http://site.net/_s/upload/2013/11/29/0be99469a67aaf35b30b236e8ee9faa3.jpg225.jpg,附属于src标记。

我做的是:

function imgSrc($content){ 
    $srcPos = strpos($content, 'src');
    $srcCut = substr($content, $srcPos+5);

    return $srcCut;
}

该功能的结果是http://site.net/_s/upload/2013/11/29/0be99469a67aaf35b30b236e8ee9faa3.jpg225.jpg' title='Autumn, tree leafs and big clock' alt='Autumn, tree leafs and big clock'/>

但我想要的只是http://site.net/_s/upload/2013/11/29/0be99469a67aaf35b30b236e8ee9faa3.jpg225.jpg

2 个答案:

答案 0 :(得分:1)

试试这个

echo rtrim(strstr($str," title",true),"'");

function imgSrc($content){ 
    $srcPos = strpos($content, 'src');
    $srcCut = substr($content, $srcPos+5);
    $srcCut = rtrim(strstr($srcCut," title",true),"'"); //Added here

    return $srcCut;
}

答案 1 :(得分:0)

你应该查看正则表达式。永远有效:)

$string = "Autumn, tree leafs<BR/><BR/><a title='Autumn, tree' href='http://site.net/share-picture/3K'><img width='225px' src='http://site.net/_s/upload/2013/11/29/0be99469a67aaf35b30b236e8ee9faa3.jpg225.jpg' title='Autumn, tree leafs' alt='Autumn, tree leafs'/></a><br/>";

$count = preg_match_all('/src=(["\'])(.*?)\1/', $string, $match);
if ($count === FALSE) {
    print 'Nada\n';
} else { 
    print_r($match[2]);
}