Link to home
Start Free TrialLog in
Avatar of maccaj51
maccaj51Flag for Afghanistan

asked on

php and preg replace

Hi Experts,

Why does this add two << around the preg replace...

I just want to add strong to first para only

$text = preg_replace('<p>', '<p><strong>', $text, 1);
$text = preg_replace('</p>', '</strong></p>', $text, 1);

Cheers
ASKER CERTIFIED SOLUTION
Avatar of hernst42
hernst42
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Unlike ereg() that has been deprecated and will be removed, the PCRE expressions require a delimiter.  Certain pieces of information for the regex follow the trailing delimiter.  For example, you can make the expression case-insensitive by adding the letter "i" at the end, after the final delimiter.

You may also find that certain "meta characters" need to be escaped by prepending them with a backslash.  PHP is inconsistent in its application of this rule.  Enlightening reading here:
http://php.net/manual/en/reference.pcre.pattern.syntax.php
Avatar of maccaj51

ASKER

Hi Ray,

Thanks for the information. I would class myself a little above beginner so will have a look...

Is there any reason why i should accept and use hernst42 answer?
Sure!  Just looking at it, I am fairly sure it works.  In some cases the wickets < and > may be treated as meta-characters, but this is not one of them.
Many Thanks hernst42

& Ray!