Author Topic: [SOLVED] function to apply nl2br to $text, but not to <pre> tags, not working right...  (Read 623 times)

0 Members and 1 Guest are viewing this topic.

Offline DonCullenTopic starter

  • Irregular
  • Posts: 4
    • View Profile
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:

Code: [Select]
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:

Code: [Select]
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:

Code: [Select]
#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.
« Last Edit: September 16, 2007, 12:21:06 AM by DonCullen »

Offline php_joe

  • Enthusiast
  • Posts: 174
    • View Profile
    • DarkSwordsGuide
Code: [Select]
I'm not sure how it would work,
but couldn't you use nl2br() to change the lines into <br />
and then use preg_match() to replace the <br /> between
the <pre> and </pre> back into \n?

Offline wildteen88

  • Guru
  • 'Insane!'
  • *
  • Posts: 12,021
  • Gender: Male
    • View Profile
Try:
Code: [Select]
<?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;

?>
« Last Edit: September 16, 2007, 12:13:05 PM by wildteen88 »

Offline DonCullenTopic starter

  • Irregular
  • Posts: 4
    • View Profile
Thanks wildteen. Found a solution. But I modified it to include some of your code. Here's my solution:

Code: [Select]
#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;
}