【问题标题】:How to test if a HTML body contains empty tags如何测试 HTML 正文是否包含空标签
【发布时间】:2015-07-20 08:51:57
【问题描述】:

我在某些表单域上使用文本编辑器,TinyMCE。它工作正常。

然而,TinyMCE 编辑器为表单中的每个字段返回一个 HTML 正文标记。如果用户完成该表单字段,这很好,即;

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

但是,如果用户没有填写 TinyMCE STILL 字段,则会返回一个包含空 html 正文的字符串,即以下代码:

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

</body>
</html>'

我要做的是测试这个返回的“表单字段”是否只包含空的 HTML 字段,即 html 字段的正文中没有值。

总而言之,我怎样才能测试上面的代码只包含空的正文标签?

【问题讨论】:

  • “tinymice”是指“tinymce”对吧?
  • 嗨,Juhana。是的。这正是我的意思。

标签: php tinymce


【解决方案1】:

它不是一个非常优雅的解决方案,但它确实有效。我只需使用 strip_tag() 从文档中删除标签,然后测试变量是否为空。如果它不为空,我提交 post 变量。

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】:

    你可以在客户端使用 jquery 来做

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

    您可以验证并向服务器发送适当的响应

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 2012-03-26
    • 1970-01-01
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多