Jump to content

[SOLVED] Need help with preg_replace regular expression


HowdeeDoodee

Recommended Posts

This preg_replace replaces or highlights key words typed in by the user.

 

Since I do not know how the regex part of the preg_replace works, I need help. Currently the replace with highlight replaces only the word typed in by the user. The replace ignores hyperlinks which I want to maintain. What I want the replace to do is replace or highlight both a whole word or a part of a word if the part of the word matches what is typed in by the user. See examples below.

 

$Topic = preg_replace ( "/(?!(?:[^<]+>|[^>]+\<\/a\>))\b(" . $highlight . ")\b/is", "<span style='background-color: #CCFF66'>\\1</span>", $Topic); 

 

As an example only, if the user types in "wacky" without the quotes, I want the preg_replace to replace or highlight the word "wacky" in the words

 

 

wacky

 

wackywoky

 

weekywacky

 

weekywackywokey

 

Currently, if any word appears inside a hyperlink, the word inside a hypelink is totally ignored which I want to maintain.

 

Here is another example.

 

User types in "impress"

 

I want the highlighter to highlight the word "impress" inside the word "impression."

 

My search engine is set up to find words using wildcard characters. Using wildcard characters, the search engine finds words inside of words, like "power" inside of "powerful." If the highlighter will highlight words within words as well as single words, any word found by the search engine will also be highligted, even if the word is a part of a word.

 

Thank you in advance for any replies.

Link to comment
Share on other sites

Try this:

 

$highlight = 'wacky';
$Topic = 'Today is quite wacky, indeed, it might be said to be weekywackywocky if I may say so.  See <a href="/wackybacky.html">here</a> for details.';
$Topic = preg_replace ( "/(?!(?:[^<]+>|[^>]+\<\/a\>))\b(\w*" . $highlight . "\w*)\b/is", "<span style='background-color: #CCFF66'>\\1</span>", $Topic);
print "$Topic\n";

 

\b means "word boundary", and \w means "word character".

Link to comment
Share on other sites

Thank you for the reply. I have highlighted words in this post to show what I was hoping for. I apologize for not thinking of doing this before.

 

I tried your example and found in the example the whole word "weekywackywocky" was highlighted. You will notice in my first example, I was hoping to be able to just highlight the word "wacky" within the word "weekywackywocky".

 

Another example would be the word "peacemakers".

 

If a user types in the word "peace", because of the way the search script works, the search word would be peace%. This means the script would find the word "peacemakers" as well as the word "peace" or any word beginning with the word "peace".

 

Since the user typed in the word "peace" then it would be acceptable to highlight the word "peace" within the word "peacemakers".

 

Is is possible to highlight just the word "peace" by itself in a record along with the word "peace" within the word "peacemakers" or highlight the word "peace" within any other word beginning with the word "peace"?

 

Thank you again for the response.

 

Link to comment
Share on other sites

Oh.. I get it now.  That's actually much easier to do :)

 

$highlight = 'wacky';
$Topic = 'Today is quite wacky, indeed, it might be said to be weekywackywocky if I may say so.  See <a href="/wackybacky.html">here</a> for details.';
$Topic = preg_replace ( "/(?!(?:[^<]+>|[^>]+\<\/a\>))(" . $highlight . ")/is", "<span style='background-color: #CCFF66'>\\1</span>", $Topic);
print "$Topic\n";

 

Now instead of the complicated matching of word boundaries, it just looks for the word "anywhere" (except in html anchors).

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.