I’ve read a few articles on preg_replace and still don’t understand what all the weird ]{!(‘/)[ characters mean.
Basically, I want to find the first instance of a break <br />, and replace it with a </strong><br />
Code I have: preg_replace('<br />', '</strong><br />', nl2br($row['n_message']), 1)
but I know I’m missing something in how I declare the strings <br /> and </strong>.
Any help? Thanks.
Regular Expressions
The only thing you are missing is a delimiter in your regexp pattern. I believe this can be any character; a common choice is a forward slash. But then of course you must escape your existing forward slashes. Here are two examples, using forward slash and right square bracket.
Alternative
I agree with michaeljdennis that you should use
str_replacein this case.preg_replaceis appropriate for fancy replacements, but not one as simple as this.However,
str_replacedoes not have a $limit argument. If you wish to limit the number of replacements to the first instance, do something like