You are here: highlighting regex matches « PHP Programming Language « IT news, forums, messages
highlighting regex matches

Posted by semi_evil on 05/23/07 13:37

I'm trying to achieve the following:

start form entry
user enters form data and submits ($form_data)
preg_match_all data for $pattern, store matches in $match_arr
if one or more matches
display string back to user, with matched substrings in different
color/emphasis
start form entry again
else continue

my initial solution to recontructing the string:
// if 1 match
preg_replace ($pattern,'<em>'.$match_arr[0][0].'</em>',$form_data,1);
// if multiple matches
preg_replace ($pattern,'<em>'.$match_arr[0].'</em>',$form_data,-1);

...but the latter doesn't work the way I hoped. When the replace var is
an array $pattern also must be an array for preg_replace to use the
different array values in $replace.

Any ideas how to get around this problem in an elegant manner? Or an
alternative solution altogether? I searched for a function/solution
that tells me at which position preg_match_all() found a match, so I
can avoid the preg_replace altogether, but I haven't found any.

cheers,
Semi

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация