Re: HTML text extraction
| From: | Ashley Sheridan | Date: | Tue, 18 Aug 2009 08:41:07 +0000 |
| Subject: | Re: HTML text extraction | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-296913@lists.php.net to get a copy of this message | ||
On Tue, 2009-08-18 at 01:37 -0700, leledumbo wrote:
> Usually, a website gives preview of its articles by extracting some of the
> first characters. This is easy if the article is a pure text, but what if
> it's a HTML text? For instance, if I have the full text:
>
> <p>
> bla bla bla
> <ul>
> <li>item 1</li>
> <li>item 2</li>
> <li>item 3</li>
> </ul>
> </p>
>
> and I take the first 40 characters, it would result in:
>
> <p>
> bla bla bla
> <ul>
> <li>item
>
> As you can see, the tags are incomplete and it might break other texts below
> it (I mean, other than this preview). I need a way to solve this problem.
>
> --
> View this message in context:
> http://www.nabble.com/HTML-text-extraction-tp25020687p25020687.html
> Sent from the PHP - General mailing list archive at Nabble.com.
>
>
You could do a couple of things:
* Extract all the content and use strip_tags() to remove all the
HTML markup. In the example you gave it might look a bit odd if
the content suggests it was originally a list.
* Access the extracted content through the DOM, and grab the
textual content you need using node values. That way, you can
limit it to a specific character count of content, and with a
bit of work, you can preserve the original markup tags too
Thanks,
Ash
http://www.ashleysheridan.co.uk