| | |
Wrap new line with <code>
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: May 2009
Posts: 15
Reputation:
Solved Threads: 0
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:
An example would be something like:
PHP Syntax (Toggle Plain Text)
<pre> <code>first line here</code> <code>second line here</code> <code>third line here</code> </pre>
1
#2 Oct 17th, 2009
Hey.
You could just use str_replace to insert the <code> tags where you want them.
Output:
You could just use str_replace to insert the <code> tags where you want them.
php Syntax (Toggle Plain Text)
<?php $text = <<<HTML <pre>Line 1 Line 2 Line 3</pre> HTML; $old = array("<pre>", "\r", "\n", "</pre>"); $new = array("<pre><code>", "", "</code>\n<code>", "</code></pre>"); echo str_replace($old, $new, $text); ?>
text Syntax (Toggle Plain Text)
<pre><code>Line 1</code> <code>Line 2</code> <code>Line 3</code></pre>
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
1
#4 Oct 17th, 2009
What do you mean?
You can wrap that into a function and use whatever input text you want:
Is that what you mean?
You can wrap that into a function and use whatever input text you want:
php Syntax (Toggle Plain Text)
<?php function addCodeToPre($input) { static $old = array("<pre>", "\r", "\n", "</pre>"); static $new = array("<pre><code>", "", "</code>\n<code>", "</code></pre>"); return str_replace($old, $new, $input); } echo addCodeToPre("<pre>First\nSecond\nThird</pre>"); echo addCodeToPre($_POST['input']); echo addCodeToPre($somethingElse); ?>
Please do not ask for help in a PM. Use the forums.
And use [code] tags!
And use [code] tags!
•
•
Join Date: May 2009
Posts: 15
Reputation:
Solved Threads: 0
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
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
php Syntax (Toggle Plain Text)
function addCodeToPre($content) { static $old = array("<pre>", "\r", "\n", "</pre>"); static $new = array("<pre><code>", "", "</code>\n<code>", "</code></pre>"); return str_replace($old, $new, $content); } add_filter('the_content', 'addCodeToPre');
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.
Output:
P.S.
Where does your
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.
php Syntax (Toggle Plain Text)
<?php header("Content-Type: text/plain;"); /** * Adds <code> tags to every line in a SINGLE <pre> block. * Assumes the input is ONLY the <pre> block. */ function addCodeToSinglePre($input) { static $old = array("<pre>", "\r", "\n", "</pre>"); static $new = array("<pre><code>", "", "</code>\n<code>", "</code></pre>"); return str_replace($old, $new, $input); } /** * Adds <code> tags to every line in every <pre> block in the input. */ function addCodeToAllPre($input) { $regexp = '/(<pre>.*?<\/pre>)/ise'; $replace = 'addCodeToSinglePre("$1");'; return preg_replace($regexp, $replace, $input); } // Random example $text = <<<TEXT Before1 Before2 <pre>Inside1 Inside2 Inside3</pre> Middle1 Middle2 <pre>Inside4 Inside5 Inside6</pre> After1 After2 TEXT; // Replace and print the example text. echo addCodeToAllPre($text); ?>
text Syntax (Toggle Plain Text)
Before1 Before2 <pre><code>Inside1</code> <code>Inside2</code> <code>Inside3</code></pre> Middle1 Middle2 <pre><code>Inside4</code> <code>Inside5</code> <code>Inside6</code></pre> After1 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!
And use [code] tags!
![]() |
Similar Threads
- Illegal argument exception (Java)
- Retrieving the number that the of error line of code? (PHP)
- Error in line of code (VB.NET)
- One line of code copy char array using pointers (C++)
- Unhandled Excepton (Java)
- Forum Indicies (DaniWeb Community Feedback)
- What is the problem with this line of my code? (Visual Basic 4 / 5 / 6)
- Need help with one line of code :( (C++)
- Parse error at last line of page's code (PHP)
- Javascript: Go To Line Number (JavaScript / DHTML / AJAX)
Other Threads in the PHP Forum
- Previous Thread: sql INSERT question
- Next Thread: Help a Newb here!
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date development directory display download dynamic echo email error file files filter folder form forms function functions gc_maxlifetime google host href htaccess html image include insert integration ip java javascript joomla limit link login loop mail memmory memory menu mlm mod_rewrite multiple mysql navigation oop parse parsing paypal pdf php problem query radio random recursion regex remote script search server sessions sms snippet soap source space sql structure syntax system table thesishelp tutorial update upload url validation validator variable video web xml youtube





