duanqiang9212 2015-11-07 07:41
浏览 21

检查用户插入的字符串是否是有效的HTML

I have one textarea for user to insert html using tinymce. And i am using html2pdf library to print that content in PDF. But, sometime user inserts invalid html like this,

<p><span>test</p>

So it cause the error while printing PDF.

So what i want is to check before printing that if HTML is valid or not like this,

if(valid_html)
   PRINT_PDF
else
   strip_tags(PRINT_PDF); // removing tags from sting

For that i tried this solution also from here. But not working in many cases. I need solution for this to allow users to print pdf.

  • 写回答

1条回答 默认 最新

  • dtn913117 2015-11-07 08:04
    关注

    Parsing HTML to ensure that it is valid is not an easy task in any language - I draw your attention to two particular articles here on stackoverflow, namely this "How do you parse and process HTML/XML in PHP?" and also "RegEx match open tags except XHTML self-contained tags"

    That said, I have found that you can do some rudimentary parsing using DOMDocument but it is far from perfect - it might however be sufficient for your purposes.

    <?php
            $textarea_contents=$_POST['name_of_textarea'];
    
            $buffer = urldecode( $textarea_contents );
            $errors = array();
            $status=200;
    
            $dom=new DOMDocument();
            $dom->validateOnParse = TRUE;
            libxml_use_internal_errors( TRUE );
            $dom->loadHTML( $buffer );
            $results=libxml_get_errors();
            $dom=NULL;
            libxml_clear_errors();
    
            if( !empty( $results ) && count( $results ) > 0 ){
                /* Errors detected, prevent further processing of your pdf */
                $status=400;
                foreach( $results as $error ){
                    $errors[]=array(
                        'message'   =>  $error->message,
                        'code'      =>  $error->code,
                        'line'      =>  $error->line,
                        'level'     =>  $error->level,
                        'column'    =>  $error->column
                    );
                }
            } else {
                /* Hopefully the submitted data validated OK - create PDF */
            }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题