tags:

views:

142

answers:

2

I'd like to change <pre> with <code> and </pre> with </code>.

I'm having problem with the slash / and regex.

+3  A: 

You could just use str_replace:

$str = str_replace(array('<pre>', '</pre>'), array('<code>', '</code>', $str);

If you feel compelled to use regexp:

$str = preg_replace("/<(\/)*pre>/", "<\\1code>", $str);

If you do one per tag:

$str = preg_replace("/<pre>/", '<code>', $str);
$str = preg_replace("/<\/pre>/", '</code>', $str);

You just need to escape that slash.

NullUserException
Could you show the preg_replace splited in 2 sentences, one per tag? Thanks
Juanjo Conti
@Juanjo See edit
NullUserException
+1  A: 

You probably need to escape the /s with \s, or use a different delimiter for the expression.

Instead, though, how about using str_replace? <pre> and </pre> will be easy to match as they're not likely to contain any classnames or other attributes.

$text=str_replace('<pre>','<code>',$text);
$text=str_replace('</pre>','</code>',$text);
Alex JL