加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何从PHP中的字符串中提取URL?

发布时间:2020-12-13 21:34:50 所属栏目:PHP教程 来源:网络整理
导读:我正在使用 PHP的“simplexml_load_file”从Flickr获取一些数据. 我的目标是获取照片网址. 我能够得到以下值(分配给PHP变量): pa href="http://www.flickr.com/people/19725893@N00/"codewrecker/a posted a photo:/ppa href="http://www.flickr.com/photos
我正在使用 PHP的“simplexml_load_file”从Flickr获取一些数据.

我的目标是获取照片网址.

我能够得到以下值(分配给PHP变量):

<p><a href="http://www.flickr.com/people/19725893@N00/">codewrecker</a> posted a photo:</p>

<p><a href="http://www.flickr.com/photos/19725893@N00/2302759205/" title="Santa Monica Pier"><img src="http://farm3.static.flickr.com/2298/2302759205_4fb109f367_m.jpg" width="180" height="240" alt="Santa Monica Pier" /></a></p>

我怎样才能提取它的这一部分?

http://farm3.static.flickr.com/2298/2302759205_4fb109f367_m.jpg

万一它有帮助,这是我正在使用的代码:

<?php
$xml = simplexml_load_file("http://api.flickr.com/services/feeds/photos_public.gne?id=19725893@N00&lang=en-us&format=xml&tags=carousel");
foreach($xml->entry as $child) {
    $flickr_content = $child->content; // gets html including img url
    // how can I get the img url from "$flickr_content"???
 }
?>

解决方法

你可以放弃使用正则表达式,假设HTML的形成方式几乎保持不变,例如:

if (preg_match('/<img src="([^"]+)"/i',$string,$matches)) {
    $imageUrl = $matches[1];   
}

这是相当不健壮的,如果HTML将要改变(例如< img>标记中的参数顺序,HTML格式错误的风险等),您最好使用HTML解析器.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读