RE: highlighting searchterms as bold text

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 20 September 2007 11:34, Edward Kay wrote:

> > > Hi everyone,
> > > 
> > > I want to highlight (bold) searchterms in text.
> > > For example, for all words starting with "ethyl":
> > > a) replace "ethyl" by "<b>ethyl</b>"
> > > b) replace "Ethyl" by "<b>Ethyl</b>"
> > > c) replace "ethylene" by "<b>ethylene</b>"
> > > d) but not "methyl" by "<b>methyl</b>"
> > > 
> > > Now I use:
> > > $patterns[0] = "/ethyl/";
> > > $replacements[0] = "<b>ethyl</b>";
> > > $text = preg_replace($patterns, $replacements, $text);
> > > 
> > > This works for a) and c), but not for b) and d).
> > > Any idea how to do this ?
> > > 
> > > TIA, Cor
> > > 
> > 
> > Thanks Deniz,
> > 
> > I tried pattern /^ethyl/i but this does not highlight anything.
> > I also tried pattern /ethyl/i and this highlights all, but also
> > methyl ... Any suggestion ? 
> > 
> 
> Yes, the i pattern modifier makes the search
> case-insensitive, which is what
> you want.
> 
> The carat ^ means start of string, so would only word if the pattern
> was at the beginning of the line - not what you want.
> 
> Try something like this:
> 
> /(\s|>)ethyl(\s|<)/i
> 
> The \s means whitespace so (\s|>) means only match if ethyl
> is preceded by
> whitespace or >, i.e. a closing tag.

Or you could try the \W character class which means any "non-word" character, or possibly even better the \b assertion which means word boundary.

Cheers!

Mike

---------------------------------------------------------------------
Mike Ford,  Electronic Information Services Adviser,
JG125, The Headingley Library,
James Graham Building, Leeds Metropolitan University,
Headingley Campus, LEEDS,  LS6 3QS,  United Kingdom
Email: m.ford@xxxxxxxxxxxxxx
Tel: +44 113 812 4730          Fax:  +44 113 812 3211 


To view the terms under which this email is distributed, please go to http://disclaimer.leedsmet.ac.uk/email.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux