0

Possible Duplicate:
Highlight keywords in a paragraph

Here is another question for you. I have a small problem in php and I thought before find an extra-ordinary solution by myself there maybe an easier and faster way to solve the problem.

Assuming I have a string which contains HTML paragraph tags like:

$string="<p>Hello this is nick</p>
         <p>i need some help over here</p>
         <p></p><p>Does anyone know a solution</p>"

And an array of stings which contains some "clue" words:

$array=("Hello","nick", "help", "anyone", "solution")

I now would like to do the following: Output the $string in a browser but the "clue" words should have a special format e.g. being bold or highlighted.

What makes me find this a bit difficult is that I want to keep the paragraphs as there are. In other words I want the final output to look exactly as the original (including new lines/new paragraphs) but with some words bold

I thought I could use strip_tags to remove <p> and </p> tags and then split the returned string by spaces. So as to get an array of words. Then I would output each word individually by checking if that word is contained in the $array. If yes, then it would be outputted with a bold style.

In this way I clearly lose the notion of new paragraphs and all the paragraphs will be merged in a single one.

Is there an easy way to fix that ? For example a way to have the knowledge that e.g. word "Hello" starts in a new paragraph? Or is there something else I can do?

Community
  • 1
  • 1
GP_
  • 114
  • 2
  • 10
  • This question had a -1 value so I up-voted it since I can't see anything wrong with the question and the down-voter neglected to explain what problem he/she had with it. – Teekin Feb 05 '12 at 15:00

3 Answers3

2

Just replace the words with formatted versions of themselves. The regex below maintains the case and replaces full words only (so that for example in the word "snicker" the word "nick" inside it isn't replaced).

preg_replace( '/\b('.implode( '|', $array ).')\b/i', '<em>$1</em>', $string );
JJJ
  • 32,902
  • 20
  • 89
  • 102
  • @ Juhana thanks for your answer. I do not really understand what that regex means but I will try to find it out using google :) I just need to confirm, does the first parameter of the function ends with i? And if yes what that i means ? Also what variable is that $1 in the second parameter (if it's variable) ? – GP_ Feb 05 '12 at 15:12
  • The `i` means that it's case insensitive, so it will replace all variants: "hello", "Hello", "HELLO" and so on. The `$1` is not a variable, it stands for the found match (if it found "hello", then $1 stands for "hello"). – JJJ Feb 05 '12 at 20:02
0

Why not just replace your clue words directly ?

$string = str_ireplace(array('hello', 'nick'), array('<strong>hello</strong>', '<strong>nick</strong>'), $string);

(of course the second array passed to the function would be generated beforehand)

Lepidosteus
  • 11,779
  • 4
  • 39
  • 51
  • @ Lepidosteus I think in this way I will again have to split the $string into words and the notion of paragraph will not exist. Correct me if I am wrong – GP_ Feb 05 '12 at 15:05
  • No, Lepidosteus's code should work - just give it a try :) – Daan Feb 05 '12 at 15:57
0

use str_replace and replace the words with bold tags around them

clem
  • 3,345
  • 1
  • 22
  • 33
  • Hey stratton thanks for your answer. Can you be more precise by giving an example because I did not quite understand how i can do that? – GP_ Feb 05 '12 at 14:59
  • have a look at @Lepidosteus's answer – clem Feb 05 '12 at 15:03