Jump to content

[SOLVED] \r\n help


adamlacombe

Recommended Posts

Does anyone know of a function that will take and replace the \r\n to <br>?

I checked the manual and found nl2br() but it does not seem to work...

this is what my functions file looks like:

<?php
function unhtmlentities($string)
{
    // replace numeric entities
    $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
    $string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string);
    // replace literal entities
    $trans_tbl = get_html_translation_table(HTML_ENTITIES);
    $trans_tbl = array_flip($trans_tbl);
    return strtr($string, $trans_tbl);
}


function nl2br_pre($string, $wrap = 40) {
  $string = nl2br($string);

  preg_match_all("/<pre[^>]*?>(.|\n)*?<\/pre>/", $string, $pre1);

  for ($x = 0; $x < count($pre1[0]); $x++) {
    $pre2[$x] = preg_replace("/\s*<br[^>]*?>\s*/", "", $pre1[0][$x]);
    $pre2[$x] = preg_replace("/([^\n]{".$wrap."})(?!<\/pre>)(?!\n)/", "$1\n", $pre2[$x]);
    $pre1[0][$x] = "/".preg_quote($pre1[0][$x], "/")."/";
  }

  return preg_replace($pre1[0], $pre2, $string);
}

function clean_up($string){
$string=mysql_real_escape_string(strip_tags(htmlentities($string, ENT_QUOTES)));
$string = html_entity_decode($string);
$string = unhtmlentities($string);
$string = nl2br_pre($string);
return $string;
}
?>

any help would be great!

Thanks in advanced,

Adam

Link to comment
https://forums.phpfreaks.com/topic/174857-solved-rn-help/
Share on other sites

<?php
function unhtmlentities($string)
{
    // replace numeric entities
    $string = preg_replace('~&#38;#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
    $string = preg_replace('~&#38;#([0-9]+);~e', 'chr("\\1")', $string);
    // replace literal entities
    $trans_tbl = get_html_translation_table(HTML_ENTITIES);
    $trans_tbl = array_flip($trans_tbl);
    return strtr($string, $trans_tbl);
}


function nl2br_pre($string, $wrap = 40) {
  

  preg_match_all("/<pre[^>]*?>(.|\n)*?<\/pre>/", $string, $pre1);

  for ($x = 0; $x < count($pre1[0]); $x++) {
    $pre2[$x] = preg_replace("/\s*<br[^>]*?>\s*/", "", $pre1[0][$x]);
    $pre2[$x] = preg_replace("/([^\n]{".$wrap."})(?!<\/pre>)(?!\n)/", "$1\n", $pre2[$x]);
    $pre1[0][$x] = "/".preg_quote($pre1[0][$x], "/")."/";
  }

  return preg_replace($pre1[0], $pre2, $string);
}

function clean_up($string){
$string=mysql_real_escape_string(strip_tags(htmlentities($string, ENT_QUOTES)));
$string = html_entity_decode($string);
$string = unhtmlentities($string);
$string = nl2br_pre($string);
$string = nl2br($string); //moved to here from nl2br_pre();
return $string;
}
?>

 

A guess, try making nl2br($string); the last call to action? Only stabbing in dark... with out understanding the regular expressions being used there  :-\

Link to comment
https://forums.phpfreaks.com/topic/174857-solved-rn-help/#findComment-921534
Share on other sites

ok I thought everything was good, but I woke up this morning and went to fix the same problem with another site and I added that code:

<?php
function protect($string)
{
$string = mysql_real_escape_string($string);
return $string;
}


function unhtmlentities($string)
{
    // replace numeric entities
    $string = preg_replace('~&#38;#38;#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
    $string = preg_replace('~&#38;#38;#([0-9]+);~e', 'chr("\\1")', $string);
    // replace literal entities
    $trans_tbl = get_html_translation_table(HTML_ENTITIES);
    $trans_tbl = array_flip($trans_tbl);
    return strtr($string, $trans_tbl);
}


function nl2br_pre($string, $wrap = 40) {
  

  preg_match_all("/<pre[^>]*?>(.|\n)*?<\/pre>/", $string, $pre1);

  for ($x = 0; $x < count($pre1[0]); $x++) {
    $pre2[$x] = preg_replace("/\s*<br[^>]*?>\s*/", "", $pre1[0][$x]);
    $pre2[$x] = preg_replace("/([^\n]{".$wrap."})(?!<\/pre>)(?!\n)/", "$1\n", $pre2[$x]);
    $pre1[0][$x] = "/".preg_quote($pre1[0][$x], "/")."/";
  }

  return preg_replace($pre1[0], $pre2, $string);
}


function clean_up($string){
$string=trim(mysql_real_escape_string(strip_tags(stripslashes(htmlentities($string, ENT_QUOTES)))));
$string = html_entity_decode(unhtmlentities(nl2br_pre(nl2br($string))));
$string = preg_replace('/\r\n|\r|\n/', "<br>", $string);
$string = str_replace('\r\n', "<br>", $string);
return $string;
}

function numeric_only($string){
$string=preg_replace("/[^0-9]/","",$string);
return $string;
}
?> 

to the functions file and now im getting this error:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/media/test/includes/functions.php:48) in /home/media/test/index.php on line 5

 

on one server its fine but the other one it gives me that error.

any idea what could be causing that error?

Link to comment
https://forums.phpfreaks.com/topic/174857-solved-rn-help/#findComment-921807
Share on other sites

lines 40-50 (functions.php):

$string = str_replace('\r\n', "<br>", $string);
return $string;
}

function numeric_only($string){
$string=preg_replace("/[^0-9]/","",$string);
return $string;
}
?> 

then first 10 lines (index.php):

<?php
include ('includes/db.php');
include ('includes/functions.php');
include ('includes/global.php');
session_start();


if($sitestatus == online || $_SESSION['type'] == 1){

if($sitestatus == offline){

Link to comment
https://forums.phpfreaks.com/topic/174857-solved-rn-help/#findComment-921813
Share on other sites

okay here's the error

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/media/test/includes/functions.php:48) in /home/media/test/index.php on line 5

 

Now functions.php is outputting a space, now as soon as an output is made, you can't send a header, and session_start(), need to make a header call, thus it fails

 

see HEADER ERRORS - READ HERE BEFORE POSTING THEM

Link to comment
https://forums.phpfreaks.com/topic/174857-solved-rn-help/#findComment-921830
Share on other sites

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.