Reputation: 514
I trying to get a clean paragraph to be displayed on the website but, because of the <p></p>
in the middle of the content I am not getting the expected output. I have tried the following but none of them worked and I am still getting <p></p>
around my content.
$story = "<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>";
1. $story = strip_tags($story, '<p>');
2. $story=str_ireplace('<p>','',$story);
3. $story=str_ireplace('</p>','',$story);
4. $story = preg_replace('/<p\b[^>]*>(.*?)<\/p>/i', '', $story);
Have I missed anything in my code? My expected output would be
Answer: Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
Upvotes: 1
Views: 6008
Reputation: 38502
Try this way with str_replace()
<?php
$story = "<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>";
$result = str_replace(['<p>','</p>'],['',''],$story);
echo $result;
?>
DEMO : https://3v4l.org/nllPP
OR with strip_tags() to remove all tags, by the way with this method you can specify allowable_tags
$result = strip_tags($story);
Upvotes: 2
Reputation: 447
$story = "<p>Lorem Ipsum has been the industry's standard dummy text ever since the
1500s, when an unknown printer took a galley of type and scrambled it to make a
type specimen book.</p><p>Lorem Ipsum has been the industry's standard dummy text
ever since the 1500s, when an unknown printer took a galley of type and scrambled
it to make a type specimen book.</p>";
$story = strip_tags($story) ;
echo $story;
there is a built in function that removes all html tags in php. strip_tags()
Upvotes: 1
Reputation: 804
Use str_ireplace()
function .
$story = "<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>";
echo $text=str_ireplace('<p>','',$story);
Demo: https://3v4l.org/W0Qeb
Upvotes: 1
Reputation: 3006
PHP has built-in functions to remove the HTML tags from a string. It is better to use strip_tags()
rather than str_replace()
, Here is an example
$story = "<p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p><p>Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.</p>";
;
$a = strip_tags($story);
$b = strip_tags($story, "<strong><em>"); // can also pass the tags that you don't want to remove
Upvotes: 2