Question

The PHP function str_replace() is failing to work with the output from a mysqli_fetch_row() result. It just outputs the original string without any alterations.

Assuming $row[1] = Hello \\r\\n This needs it's own line BOO.

Code:

$result = get_posts($database, $_GET['id'], $page);

while($row = mysqli_fetch_row($result)) { 
     echo str_replace('\\r\\n', '<br>', $row[1]);
}

Output:

Hello \\r\\n This needs it's own line BOO.

But below when I attach additional string it properly replaces but the original varible doesn't.

Code:

$result = get_posts($database, $_GET['id'], $page);

while($row = mysqli_fetch_row($result)) { 
     echo str_replace('\\r\\n', '<br>', $row[1].'\\r\\n For some reason I replace.');
}

Output:

Hello \\r\\n This needs it's own line BOO. <br> For some reason I replace.

I also tried using nl2br() but the results stay the same, however, pasting the sentence as it's own string in quotes into a str_replace() works.

I assume this has to do with the original varible and I need some sort of fix for it. I am welcome to using JQuery and Javascript as a part of my solution. If more code is needed please request in a comment.

Was it helpful?

Solution

The PHP function str_replace() is failing to work with the output from a mysqli_fetch_row() result.

Nah – you are failing to feed it the correct parameter :-)

Assuming $row[1] = Hello \\r\\n This needs it's own line BOO.

If you actually have double backslashes in your data there, then you need to have the same amount of backslashes in your search expression as well.

Now do you? Let’s have a look:

str_replace('\\r\\n', '<br>', $row[1]);

Let’s take that first argument, and output it:

echo '\\r\\n';

Output: \r\n – oops, just single backslashes …?

The backslash is the escape character, and to take that special meaning from it – even in single-quoted strings – another backslash is put in front of it. So what you have here, is the first backslash escaping the second one, therefor leaving you with one single backslash in your actual parsed text literal.

So you have to double those backslashes for them to actually mean “two literal backslashes”:

echo '\\\\r\\\\n';

Ah, that outputs \\r\\n … ain’t that nice.

So, what you actually want to use for your replacing, is

str_replace('\\\\r\\\\n', '<br>', $row[1]);

(Using only three backslashes would work as well here, since the first one would mask the second then, and the third would be “stand-alone” and taken literally, because after it there is no character that has special meaning in a single-quoted string. But to avoid confusion – and just out of principle – it’s better to double them both.)

OTHER TIPS

I am guessing this has to do with the encoding of the data you are pulling from the database. Try encoding it to UTF-8 before replacing.

while($row = mysqli_fetch_row($result)) { 
    echo str_replace('\\r\\n', '<br>', utf8_encode($row[1]));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top