【发布时间】:2012-08-07 06:15:14
【问题描述】:
我正在从数据库中提取几段,并尝试将这些段落分成一个带有正则表达式和不同类的数组。但没有任何效果。
我尝试过这样做:
public function get_first_para(){
$doc = new DOMDocument();
$doc->loadHTML($this->review);
foreach($doc->getElementsByTagName('p') as $paragraph) {
echo $paragraph."<br/><br/><br/>";
}
}
但我明白了:
Warning: DOMDocument::loadHTML() [domdocument.loadhtml]: Unexpected end tag : p in Entity, line: 9 in C:\Inetpub\vhosts\bestcamdirectory.com\httpdocs\sandbox\model\ReviewContentExtractor.php on line 18
可捕获的致命错误:第 20 行的 C:\Inetpub\vhosts\bestcamdirectory.com\httpdocs\sandbox\model\ReviewContentExtractor.php 中的 DOMElement 类的对象无法转换为字符串
为什么我会收到消息,有没有一种简单的方法可以从字符串中提取所有段落?
更新:
public function get_first_para(){
$pattern="/<p>(.+?)<\/p>/i";
preg_match_all($pattern,$this->review,$matches,PREG_PATTERN_ORDER);
return $matches;
}
我更喜欢第二种方式..但它也不好用..
【问题讨论】:
-
您特别想要 DOMDocument 吗?您曾经提到过正则表达式。错误似乎是说文档无效。
-
实际上我更喜欢使用正则表达式..因为我想保留这些标签内的所有 html
标签: php