0

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.

Community
  • 1
  • 1
Himanshu Bhardiya
  • 1,047
  • 14
  • 37

1 Answers1

1

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 */
        }
?>
Community
  • 1
  • 1
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
  • in your code, i am submitting this data `

    Set Up
    Set up a row of cones 5-10 m apart.
    Instructions
    First player.
    Points

    • tap
    • Relax body
    • surfaces (inside foot, thigh.` but not validating proper. even this is invalid html it goes in else` condition.
    – Himanshu Bhardiya Nov 07 '15 at 09:42