使用 PHP,我如何从 $foo 中分离出 src 属性的内容?最终我想要的结果只是 "http://example.com/img/image.jpg"
$foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />';
使用 PHP,我如何从 $foo 中分离出 src 属性的内容?最终我想要的结果只是 "http://example.com/img/image.jpg"
$foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />';
如果您不想使用正则表达式(或任何非标准的PHP组件),则可以使用内置的DOMDocument类来实现合理的解决方案,如下所示:
<?php
$doc = new DOMDocument();
$doc->loadHTML('<img src="http://example.com/img/image.jpg" ... />');
$imageTags = $doc->getElementsByTagName('img');
foreach($imageTags as $tag) {
echo $tag->getAttribute('src');
}
?>
<?php
$foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />';
$array = array();
preg_match( '/src="([^"]*)"/i', $foo, $array ) ;
print_r( $array[1] ) ;
http://example.com/img/image.jpg
&
实体引用和数字字符引用! - bobince/src="(.*?)"/i
。 - Alix Axelimg
属性可以在开头和结尾有空格。 - XedinUnknown/[sS][rR][cC]\s*=\s*['"]([^'"]+)['"]/i
- jewelnguyen8I got this code:
$dom = new DOMDocument();
$dom->loadHTML($img);
echo $dom->getElementsByTagName('img')->item(0)->getAttribute('src');
// Create DOM from string
$html = str_get_html('<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />');
// echo the src attribute
echo $html->find('img', 0)->src;
我非常晚才了解到这个问题,但我有一个简单的解决方案尚未提到。如果您启用了simplexml,请使用simplexml_load_string
加载它,然后通过json_encode
和json_decode
进行转换。
$foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />';
$parsedFoo = json_decode(json_encode(simplexml_load_string($foo)), true);
var_dump($parsedFoo['@attributes']['src']); // output: "http://example.com/img/image.jpg"
$parsedFoo
被解析为
array(1) {
["@attributes"]=>
array(6) {
["class"]=>
string(12) "foo bar test"
["title"]=>
string(10) "test image"
["src"]=>
string(32) "http://example.com/img/image.jpg"
["alt"]=>
string(10) "test image"
["width"]=>
string(3) "100"
["height"]=>
string(3) "100"
}
}
我已经使用它几个月来解析XML和HTML,效果很不错。目前还没有遇到过任何问题,尽管我还没有用它来解析大文件(我想使用json_encode
和json_decode
这样的方法会随着输入大小的增加而变慢)。虽然有些复杂,但这无疑是读取HTML属性最简单的方式。
这是我最终采取的方法,但我不确定它有多高效:
$imgsplit = explode('"',$data);
foreach ($imgsplit as $item) {
if (strpos($item, 'http') !== FALSE) {
$image = $item;
break;
}
}
<?php
$html = '
<img border="0" src="/images/image1.jpg" alt="Image" width="100" height="100" />
<img border="0" src="/images/image2.jpg" alt="Image" width="100" height="100" />
<img border="0" src="/images/image3.jpg" alt="Image" width="100" height="100" />
';
$get_Img_Src = '/<img[^>]*src=([\'"])(?<src>.+?)\1[^>]*>/i'; //for get img src path only...
preg_match_all($get_Img_Src, $html, $result);
if (!empty($result)) {
echo $result['src'][0];
echo $result['src'][1];
}
如果需要获取图片路径和alt文本 那么请使用下面的正则表达式,而不是上面的...
<img[^>]*src=(['"])(?.+?)\1[^>]alt=(['"])(?.+?)\2>
$get_Img_Src = '/<img[^>]*src=([\'"])(?<src>.+?)\1[^>]*alt=([\'"])(?<alt>.+?)\2*>/i'; //for get img src path & alt text also
preg_match_all($get_Img_Src, $html, $result);
if (!empty($result)) {
echo $result['src'][0];
echo $result['src'][1];
echo $result['alt'][0];
echo $result['alt'][1];
}
我从这里,PHP抽取链接自href标记得到了这个伟大解决方案的想法。
如果只想提取特定域名的网址,请尝试以下正则表达式
// for e.g. if you need to extract onlt urls of "test.com"
// then you can do it as like below regex
<a[^>]+href=([\'"])(?<href>(https?:\/\/)?test\.com.* ?)\1[^>]*>
function getTextBetween($start, $end, $text) { $start_from = strpos($text, $start); $start_pos = $start_from + strlen($start); $end_pos = strpos($text, $end, $start_pos + 1); $subtext = substr($text, $start_pos, $end_pos); return $subtext; } $foo = '<img class="foo bar test" title="test image" src="http://example.com/img/image.jpg" alt="test image" width="100" height="100" />'; $img_src = getTextBetween('src="', '"', $foo);
我使用preg_match_all来捕获HTML文档中的所有图像:
preg_match_all("~<img.*src\s*=\s*[\"']([^\"']+)[\"'][^>]*>~i", $body, $matches);
这个允许更加轻松的声明语法,可以使用不同类型的引号和空格。
正则表达式读起来像 <img (任何属性例如style或border)src(可能有空格)=(可能有空格)('或")(任何非引号符号)('或")(任何内容直到>)(>)。
'/< \s* img [^\>]* src \s* = \s* [\""\']? ( [^\""\'\s>]* )/'