如何提取

hulabang 2017-06-27 12:24:25
举例$body=“<p>中国。<img src="http://p31.pstatp.com/large/2a4300005da227b88e87" img_width="448" img_height="252" inline="0" style="border: 0px; max-width: 100%; display: block; margin: 10px auto;"/></p><p>在古代”;

那我该如何提取$body中IMG标签中src地址?该地址可能有jpg等后缀,也可能没有。谢谢各位大神!
...全文
640 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hulabang 2017-06-29
  • 打赏
  • 举报
回复
可能是我连问题都不会问吧,谢谢各位大神
hulabang 2017-06-29
  • 打赏
  • 举报
回复
大神们帮忙看看吧,我真搞不定了。。。
一起混吧 2017-06-29
  • 打赏
  • 举报
回复
你再加一句替换掉引号和反斜杠就是了 echo trim(stripslashes($m[0][0]), '"');
hulabang 2017-06-28
  • 打赏
  • 举报
回复
谢谢大神,我就是只想提取http://p1.pstatp.com/large/1b7500018624e6abe178,这个URL,而不带前面的引号和反斜杠。 另外,这个正则怎么解释啊?
xuzuning 2017-06-28
  • 打赏
  • 举报
回复
$body =<<< TXT
<img src="\"http://p1.pstatp.com/large/1b7500018624e6abe178\"" img_width="\"505\"" img_height="\"309\"" alt="\"马云与同学吃饭出场方式让人意外" 十年聚会成马云的独角戏\"="" style="\"border:" 0px;="" max-width:="" 100%;="" display:="" block;="" margin:="" 10px="" auto;\"="">
TXT;
preg_match_all('/(?<=src=)\S+/', $body, $m);
print_r($m);

Array
(
    [0] => Array
        (
            [0] => "\"http://p1.pstatp.com/large/1b7500018624e6abe178\""
        )

)
但由于你的画蛇添足,这已经不是 url 了
pilifenghuang 2017-06-28
  • 打赏
  • 举报
回复
CSDN确实比较强大的,什么问题基本上都可以解决,有很多大神级别的在里边
hulabang 2017-06-27
  • 打赏
  • 举报
回复
没贴对,$body应该是这样的: <img src="\"http://p1.pstatp.com/large/1b7500018624e6abe178\"" img_width="\"505\"" img_height="\"309\"" alt="\"马云与同学吃饭出场方式让人意外" 十年聚会成马云的独角戏\"="" style="\"border:" 0px;="" max-width:="" 100%;="" display:="" block;="" margin:="" 10px="" auto;\"="">
hulabang 2017-06-27
  • 打赏
  • 举报
回复
谢谢大神jam00 。可能是我的问题有点不对,我的$body=<img src="\"http://p1.pstatp.com/large/1b7500018624e6abe178\"" img_width="\"505\"" img_height="\"309\"" alt="\"马云与同学吃饭出场方式让人意外" 十年聚会成马云的独角戏\"="" inline="\"0\"" style="\"border:" 0px;="" max-width:="" 100%;="" display:="" block;="" margin:="" 10px="" auto;\"=""> 我肯定会给分的。如果这样的话,这个正则该怎么写呢?还有能帮我解释一下么?研究半天没明白。谢谢大神
  • 打赏
  • 举报
回复
<html>
<head>

</head>
<body>
<img id="s" src="https://ss0.baidu.com/73F1bjeh1BF3odCf/it/u=2136081071,3185426567&fm=73">
<script>
alert('ss');
var ss = document.getElementById('s').src;
alert(ss);
</script>
</body>
</html>
这样?
果酱很好吃 2017-06-27
  • 打赏
  • 举报
回复

$body='<p>中国。<img src="http://p31.pstatp.com/large/2a4300005da227b88e87" img_width="448" img_height="252"  inline="0" style="border: 0px; max-width: 100%; display: block; margin: 10px auto;"/></p><p>在古代';

preg_match_all('#<img\s*src="([^"]+?)"[^/]*?/>#',$body,$m);
print_r($m[1]);
/*
Array
(
    [0] => http://p31.pstatp.com/large/2a4300005da227b88e87
)
*/
hulabang 2017-06-27
  • 打赏
  • 举报
回复
我试试看,谢谢大神。

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧