Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove html tags

Tags:

php

strip-tags

Currently, I use strip_tags, to remove all html tags from the strings I process. However, I notice lately, that it joins words, which contained in the tags removed ie

$str = "<li>Hello</li><li>world</li>";
$result = strip_tags($str);
echo $result;
(prints HelloWorld)

How can you get around this?

like image 841
Thomas Avatar asked Feb 23 '23 04:02

Thomas


1 Answers

This would replace all html tags (anything in the form of < ABC >, in fact, without check if it truly is html) with a whitespace, then replace possible double whitespaces to single whitespaces and remove starting or ending whitespaces.

$str = preg_replace("/<.*?>/", " ", $str);
$str = trim(str_replace("  ", " ", $str));
like image 154
kontur Avatar answered Mar 07 '23 03:03

kontur