首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >php按段落拆分字符串不起作用

php按段落拆分字符串不起作用
EN

Stack Overflow用户
提问于 2018-06-23 23:58:24
回答 4查看 125关注 0票数 1

我在按段落拆分字符串时遇到了一些问题。

我在$item->description中有一个字符串,但是它似乎没有做我需要的事情。

我的代码:

代码语言:javascript
复制
$text = explode("</p>", $item->description);
var_dump($text);

它只输出所有文本的位置,而不进行拆分:

代码语言:javascript
复制
array(1) { [0]=> string(726) "<b>Requirements</b> <p>A zeal towards learning to learn 
as a priority. A sound knowledge of Internet and Browsing!</p> <b>Description</b> <p>The 
course DIGITAL SKILLS FOR TEACHERS inherits the requirements for being a ROCKSTAR 
Teachers with competency towards excellence within classrooms on narration with experience 
and making a wow feature a continuous activity in classrooms on priority. The course 
modules the expertise to imbibe for all teachers towards excellence with the adoption 
of technology in a big very way!</p> <b>Who is the target audience?</b> <p>Any individual 
who feels teaching is Sharing. A must for all Educators.</p>" }

有人能帮我吗?谢谢!

EN

回答 4

Stack Overflow用户

发布于 2018-06-24 01:35:20

检查这个:-

代码语言:javascript
复制
$text = $item->description ;
$arr = explode("</p>", $text);
echo "<br>".$arr[0] ;

希望它不会出现任何问题。

票数 0
EN

Stack Overflow用户

发布于 2018-06-24 18:02:06

如果你想得到纯文本,那么你可以使用strip_tags函数。它将从你的文本中删除所有的html标签,包括段落标签。

例如:

代码语言:javascript
复制
/** Replace the <b> tag with placeholder */
$text = str_replace("<b>", "$", $item->description);
/** Replace the <\b> tag with placeholder */
$text = str_replace("<\b>", "^", $item->description);
/** Remove html from the text */
$text = strip_tags($text);
/** Replace placeholder with <b> tag */
$text = str_replace("$", "<b>", $item->description);
/** Replace placeholder with <\b> tag */
$text = str_replace("^", "<\b>", $item->description);

或者,您可以使用preg_match_all函数。它将提取段落标签之间的所有文本。例如:

代码语言:javascript
复制
/** The text between paragraph tags is extracted using regular expression */
preg_match_all("/<p>(.*)<\/p>/iU", $item->description, $matches);
/** Each element of $text array contains text within a paragraph */
$text  = $matches[1];
票数 0
EN

Stack Overflow用户

发布于 2018-06-24 00:12:44

使用以下格式:-

代码语言:javascript
复制
$text = explode(PHP_EOL, $item->description);

希望这能行得通。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51002803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档