我可以从mysql中提取html段落而不是计算字符吗?

时间:2012-06-12 15:51:32

标签: php mysql html text-extraction

我正在使用此代码从数据库中提取文本,但效果很好。我可以只提取第一段或第二段,而不是计算到640个字符吗?

$this->data['getshorty'] = utf8_substr(html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8'), 0, 640);

1 个答案:

答案 0 :(得分:0)

如果您的段落位于<p></p>标记中,则这是一个简单的解决方案:

$fullDescription = html_entity_decode($product_info['description'], ENT_QUOTES, 'UTF-8');
preg_match('/<p\s*>(.)*<\/p\s*>/isU',$fullDescription,$matches);
$this->data['getshorty'] = $matches[0];

希望第二行的正则表达式对你有意义,请问是否!

虽然使用正则表达式解析HTML通常被认为是不好的做法,但我认为这样一个简单的例子很好。