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



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

我里面有一根$item->description,但它似乎没有做我需要的。

我的代码:

$text = explode("</p>", $item->description);
var_dump($text);

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

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>" }

有人可以帮助我吗?谢谢!

检查这个:-

$text = $item->description ;
$arr = explode("</p>", $text);
echo "<br>".$arr[0] ;

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

如果要获取纯文本,则可以使用strip_tags函数。它将从文本中删除所有 html 标签,包括段落标签。

例如:

/** 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功能。它将提取段落标签之间的所有文本。例如:

/** 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];

使用以下格式:-

$text = explode(PHP_EOL, $item->description);

希望这会起作用.

你也可以使用这段代码:-

$text = preg_split('/rn|r|n/', $item->description) ;
echo $text[0] ;

最新更新