How to test if a HTML body contains empty tags

丶灬走出姿态 提交于 2019-12-20 06:16:22

问题


I am using a text editor on certain form fields, TinyMCE. it works ok.

However the TinyMCE editor returns a HTML body tag for each field in the form. This is perfectly fine if a user completes that form field, i.e;

'description' => string '<!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    a users response
    </body>
    </html>'

However, if a user does not complete the field TinyMCE STILL returns a string containing an empty html body, i.e the code below:

 'description' => string '<!DOCTYPE html>
<html>
<head>
</head>
<body>

</body>
</html>'

What I want to do is to test if this returned 'form field' contains just empty HTML field, i.e no values within the body of the html field.

In summary how can I test that the above code just contains empty body tags?


回答1:


its not a very ellegant solution but it works. i simply remove the tags from the document using the strip_tag(), then test if the variable is empty. If its not empty i submit the post variable.

if (isset($_POST['description']))
                {
                    $preValidated = dsf_db_prepare_input($_POST['description']);
                    $testValue  = trim(strip_tags($preValidated));

                    if(!strlen($testValue) >= 1)
                    {
                        $description ='';
                     }
                     else
                     {
                        $description ="$preValidated";
                     }     
                }



回答2:


you can do it on the client side using jquery

if($('<html><body></body></html>').find('body').children().length < 1) {
  console.log('empty')
} else {
  console.log('not empty');
}

you can validate and send appropriate response to the server



来源:https://stackoverflow.com/questions/31512309/how-to-test-if-a-html-body-contains-empty-tags

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!