我在按段落拆分字符串时遇到了一些问题。
我在$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>" }
有人能帮我吗?谢谢!
发布于 2018-06-24 01:35:20
检查这个:-
$text = $item->description ;
$arr = explode("</p>", $text);
echo "<br>".$arr[0] ;
希望它不会出现任何问题。
发布于 2018-06-24 18:02:06
如果你想得到纯文本,那么你可以使用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];
发布于 2018-06-24 00:12:44
使用以下格式:-
$text = explode(PHP_EOL, $item->description);
希望这能行得通。
https://stackoverflow.com/questions/51002803
复制相似问题