2

I have a function that once in a while gives out an error:

Warning: preg_replace() [function.preg-replace]: Unknown modifier 'd'

Most often it does not. I don't understand the pattern when it does and when it doesn't.

I know almost nothing about regex, so any help would be greatly appreciated.

Here's the function:

function textHighlight($haystack,$needle,$clr='yellow') {
    $haystack=preg_replace("/($needle)/i","<span style='background:$clr;'>\${1}</span>",$haystack);
    return $haystack;
}

Thank you.

Uno Mein Ame
  • 1,060
  • 5
  • 16
  • 29

1 Answers1

3

You may have a '/' character in the $needle variable. You can replace /../ with #...#

kapitanluffy
  • 1,269
  • 7
  • 26
  • 54
  • Thank you! this worked. For my future education - is there ANY functional difference between enclosing a string in /s as opposed to #s? – Uno Mein Ame Feb 23 '13 at 05:31
  • 1
    No, AFAIK there is none actually. The regex engine is just getting confused because you have another '/' character in the $needle. I think you can escape each character with '/\' (I'm not pretty sure with that). – kapitanluffy Feb 23 '13 at 05:36