Wrap new line with <code>

Thread Solved

Join Date: May 2009
Posts: 15
Reputation: seanooi is an unknown quantity at this point 
Solved Threads: 0
seanooi seanooi is offline Offline
Newbie Poster

Wrap new line with <code>

 
0
  #1
Oct 17th, 2009
Is it possible to write a php function that wraps every newline inside a <pre></pre> block with a <code></code> block?

An example would be something like:
  1. <pre>
  2. <code>first line here</code>
  3. <code>second line here</code>
  4. <code>third line here</code>
  5. </pre>
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 454
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
1
  #2
Oct 17th, 2009
Hey.

You could just use str_replace to insert the <code> tags where you want them.

  1. <?php
  2. $text = <<<HTML
  3. <pre>Line 1
  4. Line 2
  5. Line 3</pre>
  6. HTML;
  7.  
  8. $old = array("<pre>", "\r", "\n", "</pre>");
  9. $new = array("<pre><code>", "", "</code>\n<code>", "</code></pre>");
  10.  
  11. echo str_replace($old, $new, $text);
  12. ?>
Output:
  1. <pre><code>Line 1</code>
  2. <code>Line 2</code>
  3. <code>Line 3</code></pre>
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 15
Reputation: seanooi is an unknown quantity at this point 
Solved Threads: 0
seanooi seanooi is offline Offline
Newbie Poster
 
0
  #3
Oct 17th, 2009
wow, thank you!
But is there any way to let the function search for the <pre></pre> block automatically rather than keying it in manually?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 454
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
1
  #4
Oct 17th, 2009
What do you mean?

You can wrap that into a function and use whatever input text you want:
  1. <?php
  2. function addCodeToPre($input) {
  3. static $old = array("<pre>", "\r", "\n", "</pre>");
  4. static $new = array("<pre><code>", "", "</code>\n<code>", "</code></pre>");
  5. return str_replace($old, $new, $input);
  6. }
  7.  
  8. echo addCodeToPre("<pre>First\nSecond\nThird</pre>");
  9. echo addCodeToPre($_POST['input']);
  10. echo addCodeToPre($somethingElse);
  11. ?>
Is that what you mean?
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 15
Reputation: seanooi is an unknown quantity at this point 
Solved Threads: 0
seanooi seanooi is offline Offline
Newbie Poster
 
0
  #5
Oct 17th, 2009
yeah, that's what I meant.

I just tried it and it didn't went the way i hope it would. It started wrapping every new line in <code></code> blocks.

The way I used it is
  1. function addCodeToPre($content) {
  2. static $old = array("<pre>", "\r", "\n", "</pre>");
  3. static $new = array("<pre><code>", "", "</code>\n<code>", "</code></pre>");
  4. return str_replace($old, $new, $content);
  5. }
  6.  
  7. add_filter('the_content', 'addCodeToPre');
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 454
Reputation: Atli is on a distinguished road 
Solved Threads: 56
Atli's Avatar
Atli Atli is offline Offline
Posting Pro in Training
 
1
  #6
Oct 17th, 2009
Ahh ok.
That example assumes that the entire content of the input is within <pre> tags, so it won't work on text that only has a small part wrap in them.

For that you would either need to look for the <pre> tags, extract them and then replace them with the altered content, or use regular expressions to do that.

You can use a second function that searches for <pre> blocks, using the preg_replace function, and calls the first function to wrap the lines within it in <code> tags.
  1. <?php
  2. header("Content-Type: text/plain;");
  3.  
  4. /**
  5.  * Adds <code> tags to every line in a SINGLE <pre> block.
  6.  * Assumes the input is ONLY the <pre> block.
  7.  */
  8. function addCodeToSinglePre($input) {
  9. static $old = array("<pre>", "\r", "\n", "</pre>");
  10. static $new = array("<pre><code>", "", "</code>\n<code>", "</code></pre>");
  11. return str_replace($old, $new, $input);
  12. }
  13.  
  14. /**
  15.  * Adds <code> tags to every line in every <pre> block in the input.
  16.  */
  17. function addCodeToAllPre($input) {
  18. $regexp = '/(<pre>.*?<\/pre>)/ise';
  19. $replace = 'addCodeToSinglePre("$1");';
  20. return preg_replace($regexp, $replace, $input);
  21. }
  22.  
  23. // Random example
  24. $text = <<<TEXT
  25. Before1
  26. Before2
  27. <pre>Inside1
  28. Inside2
  29. Inside3</pre>
  30. Middle1
  31. Middle2
  32. <pre>Inside4
  33. Inside5
  34. Inside6</pre>
  35. After1
  36. After2
  37. TEXT;
  38.  
  39. // Replace and print the example text.
  40. echo addCodeToAllPre($text);
  41. ?>
Output:
  1. Before1
  2. Before2
  3. <pre><code>Inside1</code>
  4. <code>Inside2</code>
  5. <code>Inside3</code></pre>
  6. Middle1
  7. Middle2
  8. <pre><code>Inside4</code>
  9. <code>Inside5</code>
  10. <code>Inside6</code></pre>
  11. After1
  12. After2

P.S.
Where does your add_filter function come from? Wordpress?
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 15
Reputation: seanooi is an unknown quantity at this point 
Solved Threads: 0
seanooi seanooi is offline Offline
Newbie Poster
 
0
  #7
Oct 18th, 2009
It works perfectly! Thank you so much!
Yeah, the add_filter came from Wordpress. I just discovered the beauty of the functions.php file.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC