1

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.

  • Does the $story contain only `

    ` html tag and no other HTML tag?

    – nice_dev Feb 15 '19 at 06:24
  • 7
    If you pass `

    ` as the second argument to `strip_tags()`, you're telling the function to _allow_ that tag, not to remove it. If you want to strip all tags, just omit the second argument. If you want to keep other html tags, that's the ones you should pass as a second argument. [Here's the manual](http://php.net/manual/en/function.strip-tags.php)

    – M. Eriksson Feb 15 '19 at 06:24
  • What is your expected output? – nice_dev Feb 15 '19 at 06:25
  • @vivek_23 I do have `` tag in content but for now, I just want to remove `

    ` tag from the content($story).

    – Alisha Lamichhane Feb 15 '19 at 06:26
  • Possible duplicate of [PHP: Strip a specific tag from HTML string?](https://stackoverflow.com/questions/3308530/php-strip-a-specific-tag-from-html-string) – Saad Suri Feb 15 '19 at 06:26

4 Answers4

2

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);
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
  • I saw the demo which is working perfectly fine but I am not getting the same output in my code. I am fetching Unicode character in the variable `$story`, is that causing me a problem? – Alisha Lamichhane Feb 15 '19 at 06:34
  • Each answer given by you is working perfectly here: https://3v4l.org/nllPP but not in my code. – Alisha Lamichhane Feb 15 '19 at 06:47
  • have a look here https://stackoverflow.com/questions/1176904/php-how-to-remove-all-non-printable-characters-in-a-string – A l w a y s S u n n y Feb 15 '19 at 06:51
  • I looked over there but no use. And, yes I tried with Unicode character outside the loop which worked but I am displaying `$story` in foreach loop and that is not working. – Alisha Lamichhane Feb 15 '19 at 07:23
2

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
Ropali Munshi
  • 2,757
  • 4
  • 22
  • 45
1

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

Nimesh Patel
  • 796
  • 1
  • 7
  • 23
1
$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()