PHP - Issues removing “\r\n\r\n” from this string: “I would like to:\r\n\r\n1.) Rid this mess\r\n\r\n\2.) Now Please”

我与影子孤独终老i 提交于 2019-12-24 10:48:55

问题


I start out with a string like this : "I would like to:\r\n\r\n1.) Rid this mess\r\n\r\n\2.) Now Please" (this is 'cleaned' user input text).

So essentially my statement would be this :

$query = sanitize($_POST['query']); // gives the result string

I want to remove the "\r\n\r\n"'s from this string. So far I have try to do this by using the following :

$query = preg_replace("/\r\n\r\n/", " ", $query);

or

$query = str_replace("\r\n\r\n", " ", $query);

None seem to work?

However, if I do the following :

$query = "I would like to:\r\n\r\n1.) Rid this mess\r\n\r\n\2.) Now Please";
$query = preg_replace("/\r\n\r\n/", " ", $query); // I tried str_replace() too
var_dump($query);
exit;

I get the output that I desire...

Could someone please explain to me why on earth this is happening and how i could solve the issue?

Any advice, input or suggestions would be greatly appreciated as I am not almost bald from pulling my hair out...

Thank you!

EDIT :

This may help function sanitize() :

function html($text)
{
    return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}

function htmlout($text)
{
    return html($text);
}

function cleanInput($input)
{
    $search = array(
        '@<script[^>]*?>.*?</script>@si',   // Strip out javascript
        '@<[\/\!]*?[^<>]*?>@si',            // Strip out HTML tags
        '@<style[^>]*?>.*?</style>@siU',    // Strip style tags properly
        '@<![\s\S]*?--[ \t\n\r]*>@'         // Strip multi-line comments
    );
    $output = preg_replace($search, '', $input);
    $output  = htmlout($output);
    return $output;
}

function sanitize($input)
{
    if (is_array($input))
    {
        foreach($input as $var=>$val)
        {
            $output[$var] = sanitize($val);
        }
    }
    else
    {
        include "C:/wamp/www/includes/inc/main/db.inc.php";
        if (get_magic_quotes_gpc())
        {
            $input = stripslashes($input);
        }
        $input  = cleanInput($input);
        $output = mysqli_real_escape_string($link, $input);
    }
    return $output;
}

回答1:


The only proper solution

  1. Get rid of sanitize() function
  2. Use prepared statements to put your data into database.
  3. use htmlout() function to display user's text back.

You can read more here and here




回答2:


Have you got magic quotes on?

http://php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc

It might be escaping your slashes.




回答3:


If there's literal \r\n characters in your database, you need to quadruple the slashes.

So you using this should in theory do the trick:

$query = preg_replace("/\\\\r\\\\n\\\\r\\\\n", " ", $query);



回答4:


This worked for me

$string = preg_replace("/\\\\r\\\\n\\\\r\\\\n/", "", $string);


来源:https://stackoverflow.com/questions/14810267/php-issues-removing-r-n-r-n-from-this-string-i-would-like-to-r-n-r-n1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!