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:

<pre>
<code>first line here</code>
<code>second line here</code>
<code>third line here</code>
</pre>
2
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by seanooi
1

Hey.

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

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

Output:

<pre><code>Line 1</code>
<code>Line 2</code>
<code>Line 3</code></pre>
0

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?

1

What do you mean?

You can wrap that into a function and use whatever input text you want:

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

Is that what you mean?

0

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

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

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.

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

Output:

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?

Votes + Comments
Excellent post! It was spot on!
0

It works perfectly! Thank you so much!
Yeah, the add_filter came from Wordpress. I just discovered the beauty of the functions.php file.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.