Re: search result highlighting / regexp voodoo
| From: | John W. Holmes | Date: | Wed, 03 Sep 2003 04:29:45 +0000 |
| Subject: | Re: search result highlighting / regexp voodoo | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-161513@lists.php.net to get a copy of this message | ||
Lee O'Mara wrote:
PROBLEM: I want to produce short "snippets" for search result pages. The search results are already partially processed (search terms get wrapped in [strong] tags), but I keep hitting dead-ends with this step. Now, I've tried a wide number of approaches, spent an embarasing amount of time and arrived at no viable solution. Learning regular expressions would probably have been less painfull, but there you have it.This should produce what you want (or darn close): preg_match_all('/(\S+\s*){0,5}<strong class="term[1-3]">([^<]+)<\/strong>(\s*\S+){0,5}/',$str,$matches); echo 'Summary: ' . implode(' ... ',$matches[0]); where $str is your text. -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals - www.phparch.com