7

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

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

4
  • In general, it's not a good idea to parse HTML with regex, but for this simple example, I suppose it couldn't hurt. Commented Jul 30, 2010 at 23:27
  • I'm modifying a wordpress plug-in and this is the way they do it. Commented Jul 30, 2010 at 23:28
  • I agree that it's fine for this example. Was just giving a heads up about deeper complexities. Commented Jul 30, 2010 at 23:32
  • 1
    As admirable WordPress the application is at meeting its goals, never assume anything related to the WordPress codebase is a good general example. Commented Jul 30, 2010 at 23:33

3 Answers 3

18

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 want to replace them separately:

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

You just need to escape that slash.

Sign up to request clarification or add additional context in comments.

2 Comments

Could you show the preg_replace splited in 2 sentences, one per tag? Thanks
@NullUserException What if there were attributes mentioned in the <pre> tag and you had to retain them exactly as they are and just replace the <pre part of it? For example, how would you change <span style="padding: 10px;">Test</span> to <div style="padding: 10px;">Test</div> ?
3

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);

4 Comments

What if there were attributes mentioned in the <pre> tag and you had to retain them exactly as they are and just replace the <pre part of it? For example, how would you change <span style="padding: 10px;">Test</span> to <div style="padding: 10px;">Test</div> ?
You just replace '<span' with '<div' and '</span>' with '<\div>'.
Thanks for the reply. I knew I could do that, but what wanted to find out how to tackle this case when I have an occurrence of the <span> tag within my body content. Because the replace will replace all occurrences and I need to replace only the class names and not all occurrences. How do I go about this?
You mean, only spans that have a certain class attribute? You'll need to use a regex. I suggest to post a new question or search for previous ones that use regex.
1

I found a very easy solution to replace multiple words in a string :

<?php
 $str="<pre>Hello world!</pre>";


$pattern=array();
$pattern[0]="/<pre>/";
$pattern[1]="/<\/pre>/";


$replacement=array();
$replacement[0]="<code>";
$replacement[1]="</code>";

echo preg_replace($pattern,$replacement,$str);?> 

output :

 <code>Hello world!</code>

With this script you can replace as many words in a string as you want :

just place the word (that you want to replace) in the pattern array , eg :

     $pattern[0]="/replaceme/"; 

and place the characters (that will be used in place of the replaced characters) in the replacement array, eg :

      $replacement[0]="new_word"; 

Happy coding!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.