首先:我有以下字符串内容
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
。
答案 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]);
}