replace array elements with highlighted text in a text

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
amitsri
Forum Newbie
Posts: 14
Joined: Fri Aug 18, 2006 6:16 am

replace array elements with highlighted text in a text

Post by amitsri »

hi,
i am trying to replace elements of an array in a text with their bold equivalent.
For example
i have this array

Code: Select all

$arr=Array('name1','name2','name3','name4','name5','name6','name7','name8','name9');
i have a text

lorem ipsum name5 lorem ipsum name2 lorem ipsum dolor name9

i want name5, name2 and name9 to be highlighted with some background color.

problem is what this is going to be time consuming if the array contains around 6000 elements i have to loop through each element and highlight the text if match is found.

Thanks in advance
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

It could be done with a simple preg_replace().

Code: Select all

$formatted = preg_replace('/(' . preg_quote(implode('|', $arrayOfWords)) . ')/ism', '<b>' . "\\1" . '</b>', $unformatted);
I was thinking it could be done with str_replace(), but I don't know how to get the second parameter to recognize the words in the first parameter's array.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
amitsri
Forum Newbie
Posts: 14
Joined: Fri Aug 18, 2006 6:16 am

Post by amitsri »

scottayy wrote:It could be done with a simple preg_replace().

Code: Select all

$formatted = preg_replace('/(' . preg_quote(implode('|', $arrayOfWords)) . ')/ism', '<b>' . "\\1" . '</b>', $unformatted);
I was thinking it could be done with str_replace(), but I don't know how to get the second parameter to recognize the words in the first parameter's array.

it gives this error:

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'P' in /home/admin/domains/netglimse.com/public_html/celeb_names.php on line 10
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

try changing the / at the beginning and end of the regex to #
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

scottayy wrote:try changing the / at the beginning and end of the regex to #
Note that by doing so, you may want to include the delimiter in the preg_quote() call as well.

Also, echo the resulting regex so we can see what the error is referring to.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

preg_quote() needs to be run before implode() unfortunately.
Post Reply