and like us on FB

When searching text for a specific keyword or keywords, it’s good practice to give the user the option of deciding whether they’d like those keywords highlighted in search results. The following PHP function will highlight defined keywords in text by regular expression.

It will turn this:

Lorem ipsum dolor sit semper amet, consectetur adipiscing elit. Semper vestibulum eu nulla auctor, vehicula sem vel, suscipit lorem ligula. Quisque libero lorem, semper eget fermentum id, condimentum at turpis.

Into this:

Lorem ipsum dolor sit semper amet, consectetur adipiscing elit. Semper vestibulum eu nulla auctor, vehicula sem vel, suscipit lorem ligula. Quisque libero lorem, semper eget fermentum id, condimentum at turpis.

PHP Functions

This is the older function we used. PHP’s ereg_replace() function was depreciated in version 5.3.0 meaning that it’s not an ideal long-term solution.

A more-up-to-date function is as follows.

Both are a little old. We’ll share others soon.

Example

Using the last function, use the following:

Shortt URL for this post: http://shor.tt/13xD