I am using CKEDITOR text editor for description. If I leave it blank or enter some spaces without text, the result comes out like this:
<p> </p> <p> </p> <p> </p> <p> </p>
The html is:
<form action="<?= base_url('Blogs/submit_post'); ?>" method="post">
<label>Description</label>
<textarea id="des" name="descr"></textarea>
</form>
PHP script is (CI4)
$data = $this->request->getVar();
$data['descr'] = htmlentities($data['descr']);
print_r($data);
Now I need that if this textarea submitted without text or white spaces like – <p> </p>
then the value of $data['descr'] should null
else the value of $data['descr'] = htmlentities($data['descr'])
How can I check that submitted textarea value is empty?
>Solution :
If you want to check if the submitted textarea value contains only non-breaking spaces and HTML tags and treat it as empty, you can also use the strip_tags function in combination with str_replace to remove HTML tags and non-breaking spaces, and then check if the resulting string is empty.
$data = $this->request->getVar();
$cleanedDescr = str_replace(' ', ' ', strip_tags($data['descr']));
if (empty(trim($cleanedDescr))) {
$data['descr'] = null;
} else {
$data['descr'] = htmlentities($data['descr']);
}
print_r($data);
This approach should also work to handle cases where the value contains only non-breaking spaces and HTML tags, treating it as empty and setting $data['descr'] to null.