I have a set of paragraphs that are inside a variable that I'd like to add to an array.
so...
$text =
'<p>para 1</p>
<p>para 2</p>
<p>para 3</p>';
outputs
Array
(
[0] => para 1
[1] => para 2
[2] => para 3
)
My Answer:
$text = '<p>para 1</p>
<p>para 2</p>
<p>para 3</p>';
// Remove HTML tags and convert entities
$text = strip_tags(html_entity_decode($text));
// Split paragraphs into an array
$paragraphs = explode("\n", $text);
// Remove empty elements
$paragraphs = array_filter($paragraphs);
// Reindex the array
$paragraphs = array_values($paragraphs);
print_r($paragraphs);
// Output:
// Array
// (
// [0] => para 1
// [1] => para 2
// [2] => para 3
// )
Rate this post
3 of 5 based on 9029 votesComments