Like Tangentially Perpendicular mentioned in the comment from earlier, and possibly like the PHP documentation might indicate in its example that contains a "\r\n"
, when I try the following code example:
<?php
// ref: https://stackoverflow.com/q/66576069/1167750
$_GET['model'] = "2 ROLLING HOOPS\r\nAND 2 SETS OF 3\r\nCORRUGATIONS;";
$model = (isset($_GET['model'])) ? $_GET['model'] : '';
// ref: https://stackoverflow.com/questions/66576069
// /php-sqlsrv-fetch-array-return-to-json#comment117691397_66576069
$model = str_replace("\r\n", " ", $model);
echo "Result:\n";
echo $model;
echo "\n";
?>
I get this output using PHP 7.3.27:
Result:
2 ROLLING HOOPS AND 2 SETS OF 3 CORRUGATIONS;
If I only use single quotes when doing str_replace('\r\n', ' ', $model);
I get the following output:
Result:
2 ROLLING HOOPS
AND 2 SETS OF 3
CORRUGATIONS;
which probably isn't what you are wanting to get as a result. Thus, I would still recommend using the double quote approach for this.
I would recommend trying a simple code example (like in this post) and seeing if you get the same result on your side. This might help narrow down if something else if affecting your results. For example, maybe there is something strange happening with your $_GET['model']
or $model
related content during the process.
Also, I would recommend using prepared statements in the near future for safety/security! It might be bad if someone were to send you strange input as the $_GET['model'] variable.
Update:
After trying an example URL similar to this with the test code from earlier in this post: http://localhost/q26.php?model=2%20ROLLING%20HOOPS\r\nAND%202%20SETS%20OF%203\r\nCORRUGATIONS;
I found that I needed to modify the following str_replace()
line to look like this:
$model = str_replace("\\r\\n", " ", $model);
in order to compensate for the way that the \r\n
appears to be being read.
So the relevant, updated example code looks like this for now:
<?php
$model = (isset($_GET['model'])) ? $_GET['model'] : '';
// ref: https://stackoverflow.com/questions/66576069
// /php-sqlsrv-fetch-array-return-to-json#comment117691397_66576069
$model = str_replace("\\r\\n", " ", $model);
echo "Result:\n";
echo $model;
echo "\n";
?>
I'm not sure why there might be a browser difference with the \r\n
part. To check, I tried the relevant, updated code from earlier with the example query and it seems to work in both Chrome (version 88.0.x) and Firefox (version 86.0.x) for me:
Chrome Result:

Firefox Result:
