I need HTML SAX (not DOM!) parser for PHP able to process even invalid HTML code. The reason i need it is to filter user entered HTML (remove all attributes and tags except allowed ones) and truncate HTML content to specified length.
Any ideas?
I need HTML SAX (not DOM!) parser for PHP able to process even invalid HTML code. The reason i need it is to filter user entered HTML (remove all attributes and tags except allowed ones) and truncate HTML content to specified length.
Any ideas?
SAX was made to process valid XML and fail on invalid markup. Processing invalid HTML markup requires keeping more state than SAX parsers typically keep.
I'm not aware of any SAX-like parser for HTML. Your best shot is to use to pass the HTML through tidy before and then use a XML parser, but this may defeat your purpose of using a SAX parser in the first place.
some comment with <b>bold text</b>, <i>italic text</i>.
it's invalid document for any XML parser. there's no root, but i don't want to mess around with wrapping content with some root element. –
Lemniscus Try to use HTML SAX Parser
Summarizing as two steps:
Use first Tidy (!), to transform "free HTML" into XHTML (or when you can not trust your "supposed XHTML"). See cleanRepair method. It needs more time, but runs with big files (!)... Set some minutes as maximum execution time if too big.
Another option (for work with big files) is to cache your XHTML files after checked or transformed into XHTML. See Tidy's repairfile method.
With a "trusted XHTML", use SAX... How to use SAX with PHP?
Parse XML with a SAX standard API, that in PHP is implemented by LibXML (see LibXML2 at xmlsoft.org), and its interface is the PHP's XML Parser, that is near to the SAX standard API.
Another way to use the "SAX of LibXML2", with another interface (a PHP iterator instead the traditional SAX interface), is to use XMLReader. See this explanation about "XMLReader use SAX".
Yes, the terms "SAX" or "SAX API" not expressed in the PHP manual (!!). See this old but good introduction.
I may suggest the pear package here : http://pear.php.net/package/XML_HTMLSax/redirected
© 2022 - 2024 — McMap. All rights reserved.