After a long long time, i get time to write an article and I am curious to write but didn’t get time. So, Today I am going to explain about how to clean HTML content using PHP code.
Sometimes you want to remove HTML tags from your content.Especially when you want to display short description from the long description of content, following function is useful.It is very useful when we want to completely remove HTML code from the string.
Let’s see function to clean html content
1 2 3 4 5 6 7 | function cleanContent($content) { $content = nl2br($content); $content = preg_replace('#(?:<br\s*/?>\s*?){2,}#', ' ',$content); return trim(strip_tags($content); } |
Super Simple! This function can be very helpful to strip out all HTML tags.Here, An above little code snippet will take the sting and then used nl2br function which will replace \ n to HTML < br /> tag. then I have used preg_replace function which will replace all
tag to space. then trim and strip_tags which remove extra space and other HTML content respectively.
That’s it. Place above function into a file and call function and see magic ;).
Hope this helps someone else out.As always,Thanks for reading. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.
Comments (1)