[K12OSN] PHP regular expression question.

Toshio Kuratomi toshio at tiki-lounge.com
Fri Mar 3 21:05:21 UTC 2006


On Fri, 2006-03-03 at 09:40 -0800, RiE wrote:
> I know, it's off topic, there are just so many knowledgable people
> here, someone will have the answer.
> I have a string.  It could look like:   (I added the under_score to
> break it so it won't display as a link in email.)
> "blah blah blah <a hr_ef=moreblah.com>link</a>...  Other link here: 
> <a hr_ef=otherlink.com"
> 
> Yes, the string could have an incomplete tag at the end.   What would
> be the solution to get rid of the last incomplete tag.  It could be an
> image tag, it could be an 'A HREF" tag.
> It's acceptable to yield the string:
> "blah blah blah <a hr_ef=moreblah.com>link</a>...  Other link here:  "
> 
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.

-Toshio

> optionally, add in '...' like this:
> "blah blah blah <a hr_ef=moreblah.com>link</a>...  Other link here:  ..."
> 
> 
> Thanks.
> 
> _______________________________________________
> K12OSN mailing list
> K12OSN at redhat.com
> https://www.redhat.com/mailman/listinfo/k12osn
> For more info see <http://www.k12os.org>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 191 bytes
Desc: This is a digitally signed message part
URL: <http://listman.redhat.com/archives/k12osn/attachments/20060303/767b83e7/attachment.sig>


More information about the K12OSN mailing list