PHP SAX parser for HTML?
Asked Answered
L

4

3

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?

Lemniscus answered 30/5, 2010 at 15:38 Comment(3)
hi, i am currently searching for such myself. i wonder if you are still using HTML SAX Parser, or if you've found something else?Futurism
Tidy is the unique "general solution" for "invalid HTML code", and PHP have a build-in good SAX (!) see my answer below.Ricker
See similar question: https://mcmap.net/q/1806109/-php-read-and-repair-big-invalid-xml-files/287948Ricker
B
4

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.

Biometrics answered 30/5, 2010 at 16:49 Comment(6)
even after tidy pieces of HTML won't be valid. they're like this: 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
@Lemniscus why do you need an event handler in the first place. If the HTML snippets are short, I see no compelling reason.Biometrics
@Lemniscus Sorry, I meant an event driven API such as SAX.Biometrics
oh, i've already got implementation using SAX parser, it's very efficient and simple, but its problem is SAX parser itself. it uses regexp to parse HTML :(Lemniscus
@Lemniscus HTML parsing with regex => troubleBiometrics
agree. thats why i'm looking for something better.Lemniscus
C
1

Try to use HTML SAX Parser

Crabbing answered 6/8, 2010 at 11:54 Comment(2)
I've tried to use it, it can't handle embedded js or complex styles because its based on regexes.Lemniscus
I used it to solve the problem that you are trying to solve. I filter user-generated content, cut JavaScript, tags, attributes.Crabbing
R
1

Summarizing as two steps:

  1. Use Tidy to transform "free HTML" into "good XHTML".
  2. Use XML Parser to parse XHTML as XML by SAX API.

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.

Ricker answered 27/7, 2013 at 22:24 Comment(0)
T
0

I may suggest the pear package here : http://pear.php.net/package/XML_HTMLSax/redirected

Torrence answered 16/4, 2012 at 15:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.