Jump to content

[SOLVED] function to apply nl2br to $text, but not to <pre> tags, not working right...


DonCullen

Recommended Posts

Basically, I wrote a function that'd take a string, apply the nl2br function to it, but also leave any content contained in between pre tags alone.

 

For example:

 

Normal content/n
Normal content/n
<pre>Pre'ed content/n
Pre'ed content/n
Pre'ed content</pre>/n
Normal content/n
Normal content/n

 

Would become:

 

Normal content<br />
Normal content<br />
<pre>Pre'ed content/n
Pre'ed content/n
Pre'ed content</pre><br />
Normal content<br />
Normal content<br />

 

This is the function I wrote:

 

	#Applies nl2br to $text, but also makes sure to avoid applying the function to any content within pre tags
function nl2brex($text) {
	if(!stripos($text, '<pre>', $prevpos)){
		$text = nl2br($text);
		return $text;
	}
	$prevpos = 0;
	$finaltext = '';
	while (strlen($finaltext) <= strlen($text)) {
		$preposition = stripos($text, '<pre>', $prevpos);
		$splittedstring = substr($text, $prevpos, $preposition);
		$splittedstring = nl2br($splittedstring);
		$finaltext .= $splittedstring.'***before pre***';
		$afterpreposition = stripos($text, '</pre>', $preposition) + 6;
		$splittedstring = substr($text, $preposition, $afterpreposition).'***after pre***';
		#msgbox($afterpreposition);
		#msgbox(strlen($text));
		$finaltext .= $splittedstring;
		$prevpos = $afterpreposition;
	}
	return $finaltext;
}

 

The function, unfortunately, doesn't work correctly. For some reason, it isn't catching the </pre> tag, and appending the ***after pre*** to the end of the entire string, instead of right after the </pre> tag. The reason why I have the *** before/after pre *** appended is to assist in debugging, when I was trying to figure out why the function wasn't operating as expected. You can see the results of the function being applied here:

 

http://bnetdocs.dementedminds.net/?op=packet&pid=237

 

Scroll down to view the first comment, and you'll see what I'm talking about.

 

If there's a better/more efficient way to accomplish the task, I'm open to alternatives.

Try:

<?php

$text = 'Original text

with newlines!

<pre>my text

should not have <br />\'s!</pre>

Ohh! More';

function restore_pre($text)
{
    $text = str_replace(array("<br />\r\n", "<br />\r", "<br />\n"), "\n", $text);

    return '<pre>' . htmlentities($text) . '</pre>';
}

// add newlines
$text = nl2br($text);

// restore pre tags to orginal state.
$text = preg_replace('/<pre>(.*)<\/pre>/ies', "restore_pre('$1')", $text);

echo $text;

?>

Thanks wildteen. Found a solution. But I modified it to include some of your code. Here's my solution:

 

	#Applies nl2br to $text, but also makes sure to avoid applying the function to any content within pre tags
function nl2brex($text) {
	$text = nl2br($text);
	$text = preg_replace('!(<pre.*?>)(.*?)</pre>!ise', " stripslashes('$1') .  stripslashes(clean_pre('$2'))  . '</pre>' ", $text);
	return $text;
}

# Remove paragraphs and breaks from within any <pre> tags.
function clean_pre($text) {
	$text = str_replace(array("<br />\r\n", "<br />\r", "<br />\n"), "\n", $text);
	return $text;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.