MuchaZ
MuchaZ

Reputation: 421

Removing new lines

I have a function in a class:

public function displayComments($postorpage, $id_postorpage){
        $query = mysql_query("SELECT * FROM `".DB."`.`mmsv_servers_page_comments` WHERE `id_postorpage`='$id_postorpage'");
        while($comment = mysql_fetch_object($query)){
        $added = new Time;
        $elapsed = $added->displayElapsedSignificant($comment->data);
         echo "
            <h3>$comment->nick</h3> 
            <i>$elapsed</i>
            <p>".addSecurityView($comment->text)."</p>
            ";
        }       
    }

And the function addSecurityView which supposes to remove the break lines doesn't work:

function addSecurityView($value)
{

    $result = preg_replace(array("/\r\n\r\n/", "/\n\n/"), array("\r\n", "\n"), $value);

    return $result;
}

Upvotes: 1

Views: 430

Answers (3)

Ali Almoullim
Ali Almoullim

Reputation: 1038

i believe this should work with you as it works with me

<?php
$trans = array("\r" => "" , "\n" => "");
echo strtr("\n hi and \r hi", $trans);
?>

and consider on changing from MySql to MySqli or PDO

Upvotes: 1

Mihai Vilcu
Mihai Vilcu

Reputation: 1967

try this one here

$string = trim(preg_replace('/\s+/', ' ', $string));

here is nice simple SECURE output function

function secureOutput($string) {

    $string = trim(preg_replace('/\s+/', ' ', $string)); //this will remove multiple whitespaces

    return htmlentities($string, ENT_QUOTES);
}

Upvotes: 0

ikonic
ikonic

Reputation: 71

Try using strtr function, with replace pairs. http://php.net/manual/en/function.strtr.php

Upvotes: 0

Related Questions