How to check if the html returned is just an empty tag.
Hi everyone. I have a chat system in place but it has an issue that If someone types just a space into the input (I use quilljs for the contentEditable) than it will record a empty p tag.
I want to check in the post create method in the only content is an empty tag so I can return early.
Or if someone has some REGEX to remove the outer tag if nothing is in it I would appriitate it. I do use REGEX to remove empty tags within the main p but the main element is not bieng filtered out, here is my current cleaner class.
<?php
namespace App\Classes;
use Stevebauman\Purify\Facades\Purify;
class HtmlCleaner
{
public static function clean($html)
{
$cleaned = Purify::clean($html);
$cleaned = str_replace(' ', ' ', $cleaned);
do {
$tmp = $cleaned;
$cleaned = preg_replace(
'#<([^ >]+)[^>]*>[[:space:]]*</>#',
'',
$cleaned
);
} while ($cleaned !== $tmp);
return $cleaned;
}
}
@Nakov Here is before and after the purify (most of the other empty tags were actually stripped out even beofre arriving at the Purify method probably by QuillJs)
at last the $cleaned will be completely empty string.. keep in mind that this removes the <p></p> just in order to see if there is any content at all in between, so you can use this for the check, but make sure that if the content is not empty then you return whatever the original one was.