简体   繁体   English

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

[英]Can I extract html paragraphs from mysql instead of counting characters?

I'm using this code to extract text from the database and it works well. 我正在使用此代码从数据库中提取文本,并且效果很好。 Can I have it extract only the first or second paragraph instead of counting to 640 characters? 我可以只提取第一段或第二段而不是计算640个字符吗?

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

Here's a simple solution that should work if your paragraphs are in <p></p> tags: 这是一个简单的解决方案,如果您的段落位于<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];

Hopefully the regex on the second line makes sense to you, ask if not! 希望第二行的正则表达式对您有意义,请问是否可以!

Although parsing HTML with regex is usually considered bad practice, I think for a simple example like this it is fine. 尽管通常使用regex解析HTML被认为是不好的做法,但我认为对于像这样的简单示例来说,这很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM