2

我有一个变量 $content ,其中包含这种形式的一些文本和图像(图像数量未知):

    text text text text <img src="path/to/image/1">text text text text
    <img src="path/to/image/2">
text text text text text text text text text text text text text text text text <img src="path/to/image/3"><img src="path/to/image/4">text text text text 
<img src="path/to/image/5">

我想提取所有图像 src 并使用 php 将它们存储在数组中,如下所示:

array(
[1]="path/to/image/1"
[2]="path/to/image/2"
[3]="path/to/image/3"
[4]="path/to/image/4"
[5]="path/to/image/5"
.
.
.
)

做这样的事情的最好方法是什么。我已经尝试过函数 explode 但这种方式似乎效率低下。

4

3 回答 3

8
    $dom = new domDocument;
    $dom->loadHTML($html);
    $dom->preserveWhiteSpace = false;
    $imgs  = $dom->getElementsByTagName("img");
    $links = array();
    for($i = 0; $i < $imgs->length; $i++) {
       $links[] = $imgs->item($i)->getAttribute("src");
    }
于 2012-07-11T19:55:11.963 回答
3

这是一个使用simplehtmldom的示例:

include("simple_html_dom.php");
$content = '
text text text text <img src="path/to/image/1">text text text text
    <img src="path/to/image/2">
text text text text text text text text text text text text text text text text <img src="path/to/image/3"><img src="path/to/image/4">text text text text 
<img src="path/to/image/5"> ';

$html = str_get_html($content);
$images = $html->find("img");
$links = array();
foreach($images as $image) {
  $links[] = $image->src;
}

print_r($links);

输出:

Array
(
    [0] => path/to/image/1
    [1] => path/to/image/2
    [2] => path/to/image/3
    [3] => path/to/image/4
    [4] => path/to/image/5
)
于 2012-07-11T19:53:10.323 回答
-1

使用正则表达式:

<?php

$str = '    text text text text <img src="path/to/image/1">text text text text
    <img src="path/to/image/2">
text text text text text text text text text text text text text text text text <img src="path/to/image/3"><img src="path/to/image/4">text text text text
<img src="path/to/image/5">';


preg_match_all('@<img.*src="([^"]*)"[^>/]*/?>@Ui', $str, $out);

print_r($out[1]);

?>

输出:

Array
(
    [0] => path/to/image/1
    [1] => path/to/image/2
    [2] => path/to/image/3
    [3] => path/to/image/4
    [4] => path/to/image/5
)
于 2012-07-11T20:06:16.237 回答