I have some HTML, and I'm extracting a snippet at a certain point (an inline image), but I'd like to show some context around this image.
I'm using PHP, and I know that both Symfony and Wordpress provide functions for dealing with what happens when you chop up text in the middle of some HTML (it closes all open tags), but nothing for dealing with snippets in the other direction.
So, in the case of :
'Snippet of text and a <a href="#moo">link right her'
I can use the above-mentioned function to fix, but what about:
'nk right here</a> and then more text after the link.'
I've considered the possibility that even the tag-closing snippet is probably the wrong way to go about this, and I should instead be using Xpath to parse the HTML. However, I can't find any examples or mentions of using xpath to create snippets like this.
Update:
So my current idea is:
move up the parse tree until I get to the tag that encloses all the content (div class=post in my case). The last node that I have before this div is the starting point (most likely a p tag).
From here, get the previous sibling (which should be a p tag again).
Descend into this node and get the last children, saving the text content to a temporary string. Keep stepping back through these children, until we get enough of a snippet.
This still ins't ideal, as I'm not sure how far I'll have to step down to get the text content.
Does anyone know of an implementation of this idea anywhere?