I'm using PHP's "simplexml_load_file" to get some data from Flickr.
我正在使用PHP的“simplexml_load_file”从Flickr获取一些数据。
My goal is to get the photo url.
我的目标是获取照片网址。
I'm able to get the following value (assigned to PHP variable):
我能够得到以下值(分配给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>
How can I extract just this part of it?
我怎样才能提取它的这一部分?
http://farm3.static.flickr.com/2298/2302759205_4fb109f367_m.jpg
Just in case it helps, here's the code I'm working with:
万一它有帮助,这是我正在使用的代码:
<?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"???
}
?>
4 个解决方案
#1
You can probably get away with using a regular expression for this, assuming that the way the HTML is formed is pretty much going to stay the same, e.g.:
你可以放弃使用正则表达式,假设HTML的形成方式几乎保持不变,例如:
if (preg_match('/<img src="([^"]+)"/i', $string, $matches)) {
$imageUrl = $matches[1];
}
This is fairly un-robust, and if the HTML is going to change (e.g. the order of parameters in the <img>
tag, risk of malformed HTML etc.), you would be better off using an HTML parser.
这是相当不健壮的,如果HTML将要改变(例如标签中的参数顺序,HTML格式错误的风险等),您最好使用HTML解析器。
#2
It's not solving your problem(and probably total overkill), but worth mentioning because I've used the library on 2 projects and it's well written.
这不是解决你的问题(也可能是完全矫枉过正),但值得一提的是因为我在2个项目中使用了这个库而且编写得很好。
phpFlickr - http://phpflickr.com/
phpFlickr - http://phpflickr.com/
#3
Easy way: Combination of substr and strpos to extract first the tag and then the src='...' value, and finally the target string.
简单方法:substr和strpos的组合首先提取标签,然后提取src ='...'值,最后提取目标字符串。
Slightly more difficult way (BUT MUCH MORE ROBUST): Use an XML parsing library such as simpleXML
稍微更困难的方式(但更强大):使用XML解析库,如simpleXML
#4
I hope this is helpful. I enjoy using xpath to cut through the XML I get back from SimpleXML:
我希望这是有帮助的。我喜欢使用xpath来切换从SimpleXML返回的XML:
<?php
$xml = new SimpleXMLElement("http://api.flickr.com/services/feeds/photos_public.gne?id=19725893@N00&lang=en-us&format=xml&tags=carousel", NULL, True);
$images = $xml->xpath('//img'); //use xpath on the XML to find the img tags
foreach($images as $image){
echo $image['src'] ; //here is the image URL
}
?>
#1
You can probably get away with using a regular expression for this, assuming that the way the HTML is formed is pretty much going to stay the same, e.g.:
你可以放弃使用正则表达式,假设HTML的形成方式几乎保持不变,例如:
if (preg_match('/<img src="([^"]+)"/i', $string, $matches)) {
$imageUrl = $matches[1];
}
This is fairly un-robust, and if the HTML is going to change (e.g. the order of parameters in the <img>
tag, risk of malformed HTML etc.), you would be better off using an HTML parser.
这是相当不健壮的,如果HTML将要改变(例如标签中的参数顺序,HTML格式错误的风险等),您最好使用HTML解析器。
#2
It's not solving your problem(and probably total overkill), but worth mentioning because I've used the library on 2 projects and it's well written.
这不是解决你的问题(也可能是完全矫枉过正),但值得一提的是因为我在2个项目中使用了这个库而且编写得很好。
phpFlickr - http://phpflickr.com/
phpFlickr - http://phpflickr.com/
#3
Easy way: Combination of substr and strpos to extract first the tag and then the src='...' value, and finally the target string.
简单方法:substr和strpos的组合首先提取标签,然后提取src ='...'值,最后提取目标字符串。
Slightly more difficult way (BUT MUCH MORE ROBUST): Use an XML parsing library such as simpleXML
稍微更困难的方式(但更强大):使用XML解析库,如simpleXML
#4
I hope this is helpful. I enjoy using xpath to cut through the XML I get back from SimpleXML:
我希望这是有帮助的。我喜欢使用xpath来切换从SimpleXML返回的XML:
<?php
$xml = new SimpleXMLElement("http://api.flickr.com/services/feeds/photos_public.gne?id=19725893@N00&lang=en-us&format=xml&tags=carousel", NULL, True);
$images = $xml->xpath('//img'); //use xpath on the XML to find the img tags
foreach($images as $image){
echo $image['src'] ; //here is the image URL
}
?>