Php 以指定格式将段落转换为数组
我有这样的Html pragraphPhp 以指定格式将段落转换为数组,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匹配特定标记
<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
)