Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/288.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 以指定格式将段落转换为数组_Php_Arrays_Domdocument - Fatal编程技术网

Php 以指定格式将段落转换为数组

Php 以指定格式将段落转换为数组,php,arrays,domdocument,Php,Arrays,Domdocument,我有这样的Html pragraph <p>This is the sample image</p><img src="test.png"/><p>this is thesample test</p> 如何使用php实现这一点。谁能给我一些建议吗。这里是示例html内容,而不是确切的内容。此内容可能会有所不同,并且具有不同的html标记。如果img来了,src应该以正确的顺序存储在数组和内容中。使用preg\u match匹配特定标记

我有这样的Html pragraph

<p>This is the sample image</p><img src="test.png"/><p>this is thesample test</p>

如何使用php实现这一点。谁能给我一些建议吗。这里是示例html内容,而不是确切的内容。此内容可能会有所不同,并且具有不同的html标记。如果img来了,src应该以正确的顺序存储在数组和内容中。

使用preg\u match匹配特定标记

$source = "<p> text line </p>";

preg_match("'<p>(.*?)</p>'si", $source, $match);

var_dump($match);
$source=“文本行”

”; preg_match(“'(.*?

'si)”,$source,$match); var_dump($match);
你会得到文本

而就所有人而言—

preg_match_all("|<[^>]+>(.*)</[^>]+>|U", $source, $match, PREG_PATTERN_ORDER);
preg_match_all(“|]+>(.*]+>|U“,$source,$match,preg_PATTERN_ORDER);

使用
DOMDocument
可以。获取无效标记的
nodeValue
属性。例如:

$html_string = '<p>This is the sample image</p><img src="test.png"/><p>this is thesample test</p>';
$dom = new DOMDocument();
$dom->loadHTML($html_string);
$elements = $dom->getElementsByTagName('*');
$a = array();
foreach($elements as $element) {
    if(in_array($element->tagName, array('html', 'body'))) continue;
    if(!empty($element->nodeValue)) {
        $a[] = $element->nodeValue;
    } else {
        foreach($element->attributes as $att) {
            $a[] = $att->value;
        }
    }
}

echo '<pre>';
print_r($a);

那是打字错误吗?第一个
没有结尾是的,我更正了。谢谢
$html_string = '<p>This is the sample image</p><img src="test.png"/><p>this is thesample test</p>';
$dom = new DOMDocument();
$dom->loadHTML($html_string);
$elements = $dom->getElementsByTagName('*');
$a = array();
foreach($elements as $element) {
    if(in_array($element->tagName, array('html', 'body'))) continue;
    if(!empty($element->nodeValue)) {
        $a[] = $element->nodeValue;
    } else {
        foreach($element->attributes as $att) {
            $a[] = $att->value;
        }
    }
}

echo '<pre>';
print_r($a);
Array
(
    [0] => This is the sample image
    [1] => test.png
    [2] => this is thesample test
)