你可以在src=
使用explode();
然后剪掉剩余=
和"
在每一端,如果你不想让他们:
<?php
$string = '<img style="max-width:100%" src="http://media.doisongphapluat.com/thumb_x670x/2015/12/26/thumon.png" alt="">';
$string = explode("src=", $string);
$string = explode(" ", $string[1]);
$string = substr($string[0], 1, -1); // remove 1st and last "
echo $string;
?>
你需要确保你的HTML没有单一引号和逃避他们,如果它确实是这样\'
最后一行删除@SeanBright的答案在这里借用了第一和最后一个字符:Delete first character and last character from String PHP
$string = explode('src=', $string);
作品 - 测试。然后您只需要用$string = substr($string[0], 1, -1); // remove 1st and last "
删除" "
。
你也可以用
$string = str_replace('"','',$string);
删除双引号的,我认为你的正则表达式是失败的,因为它是在src =后只是让一切”,但在空间不停止,其他人在别处评论这正则表达式是不是分手了HTML的最可靠方法。
如果你不希望explode();
你可以使用strpos()
和substr();
$string = '<img style="max-width:100%" src="http://media.doisongphapluat.com/thumb_x670x/2015/12/26/thumon.png" alt="">';
$pos = strpos($string,'http');
$string = substr($string,$pos, -9); // remove last " and alt-...
echo $string;
分割字符串如果图像标签到底是因为你,所以你需要使用多一点的代码使完全程序化的
这只会工作,但会失败,如果关闭/>
标签在那里:
$string = '<img style="max-width:100%" src="http://media.doisongphapluat.com/thumb_x670x/2015/12/26/thumon.png" alt="">';
$pos = strpos($string,'http'); // find position of !http!
$string = substr($string,$pos); /// get string after !http"
$len = strlen($string); // get the length of resulting string
$pos1 = strpos($string,'"'); // find last "
$difpos = $len - $pos1; // get the difference to use for the minus
$string = substr($string,0,-$difpos); // get the string from 0 to "minus" position at end.
echo $string;