[K12OSN] PHP regular expression question.
RiE
roger.in.eugene at gmail.com
Fri Mar 3 21:22:03 UTC 2006
On 3/3/06, Toshio Kuratomi <toshio at tiki-lounge.com> wrote:
> If what you're trying to accomplish is getting rid of any incomplete
> tags at the end of the string, a regex like this ought to work to detect
> the situation::
> /<[^>]*$/
> In order to extract a fixed up string you'll want something like this::
> if (preg_match('/(.*)<[^>]*$/', $string, $matches) {
> $newstring = $matches[1] . '...';
> } else {
> $newstring = $string;
> }
>
> This does not take care of the case where a tag that requires closing is
> left open at the end of the string (ie: "<a_href=blah.txt>Blah</a> ...
> <a_href=foo.txt>foo"; "<a_href=blah.txt>Blah</a> ... <table>Break me";
> etc) Solving that requires that your php script knows a little bit
> about the proper structure of html.
>
I wan't thinking about the unmatched tags(but I should have). I'm
trying to take an RSS feed, grab the summary and display just a
portion of it. some of the summaries are the entire content, I was
trying to grab just 300 characters or so. One of the test feeds
happened to have a link right at 300 that was being chopped in two..
I don't care about links or image in my summary, maybe I'll just drop
all <..> tags, plus one on the end if it's truncated <..
Thanks, I'll play around with it later. I wasn't sure about the
/<[^>]*$/ part..
Roger
More information about the K12OSN
mailing list