PHP разделить строку по абзацу не работает - PullRequest
0 голосов
/ 23 июня 2018

У меня проблемы с разбиением строки по абзацам.

У меня есть строка внутри $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 голосов
/ 24 июня 2018

Если вы хотите получить простой текст, вы можете использовать функцию 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];
0 голосов
/ 23 июня 2018

Вы также можете использовать этот код: -

$text = preg_split('/\r\n|\r|\n/', $item->description) ;
echo $text[0] ;
0 голосов
/ 23 июня 2018

Отметьте это: -

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

Надеюсь, что не возникнет никаких проблем.

0 голосов
/ 23 июня 2018

Используйте этот формат: -

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

Надеюсь, это сработает.

...