PHP按段分割字符串不起作用

时间:2018-06-23 15:58:24

标签: php split explode

我很难按段分割字符串。

我在$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>" }

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

4 个答案:

答案 0 :(得分:0)

选中此一项:-

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

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

答案 1 :(得分: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];

答案 2 :(得分:-2)

使用此格式:-

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

希望这可以。

答案 3 :(得分:-2)

您也可以使用此代码:-

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