PHP/Codeigniter 3 is changing variable values possible
I have stored \r\n in a table (for 'crlf' and 'newline') and it worked (I can see that visually in HeidiSQL), BUT when I do this:
$config = [];
foreach ($this->db->query("SELECT name, value from _email where name = 'crlf' OR name = 'newline'")->result() as $t) {
$config[$t->name] = $t->value;
if ($t->value === '\r\n'){
$config["what1"] = $t->value;
}
if ($t->value === '\\r\\n'){
$config["what2"] = $t->value;
}
}
file_put_contents("email.txt", implode("|", $config));
file_put_contents("email.json", json_encode($config));
email.txt is showing:
\r\n|\r\n|\r\n|\r\n
email.json is showing:
{
"crlf": "\\r\\n",
"what1": "\\r\\n",
"what2": "\\r\\n",
"newline": "\\r\\n"
}

On this server MySQL is version 5.7.41 and PHP is version 7.4
How this is possible?
How to accurately determine what is stored in those variables, because I think that $t->value is changed by PHP itself?
Because variables are somehow changed and I don't know their exact values. The script to send emails is not working (I am to comply with RFC 822). But if I add this just under the code:
$config["crlf"] = "\r\n";
$config["newline"] = "\r\n";
The script is sending emails normally.
SOLUTION:
I've found a working solution by using stripcslashes function:
$config = [];
foreach ($this->db->query("SELECT name, value from _email where name = 'crlf' OR name = 'newline'")->result() as $t) {
$config[$t->name] = stripcslashes($t->value);
}
Comments:
2023-01-23 23:01:07
How are you seeing those values? Please post a
minimal reproducible example that shows how you're inserting and displaying the values.
2023-01-23 23:01:07
I am getting \\r\\n instead of \r\n (\ gets escapped too)
2023-01-23 23:01:07
You already said that in the question. But nothing in PHP prints output like you show, so it's not clear how you're determining this. Show the actual code,
2023-01-23 23:01:07
try this $config[$t->name] = substr($t->value, 1)
in foreach.
2023-01-23 23:01:07
The problem is almost certainly with the code that inserts the values.
2023-01-23 23:01:07
$t->value == "\\r\\n" is hitting true I just checked. So how this is possible?
2023-01-23 23:01:07
@Barmar I've checked it more, please take a look at my question again, thanks
2023-01-23 23:01:07
where name = 'crlf' and name = 'newline'
can never be true. I think you mean or
instead of and
. But you still haven't posted the code that inserts. Didn't you comment earlier that you found the problem there?
2023-01-23 23:01:07
@Barmar OK I am changing it to OR
2023-01-23 23:01:07
That won't fix the backslash problem, it will just make the query return something.
2023-01-23 23:01:07
Please can someone explain to me why == is not working in PHP at least?
2023-01-23 23:01:07
@Barmar this query is returning two rows both \r\n I can see that in HeidiSQL
2023-01-23 23:01:07
Will you PLEASE show the code that's storing the characters into the table. I doubt very much that CI is changing the data by itself, you must be escaping it when you insert.
2023-01-23 23:01:07
The output you show is what would be expected if the data has literal backslashes in it, not CR and LF control characters. JSON escapes backslashes in strings.
2023-01-23 23:01:07
OK, I agree about that, and probably I have \\r\\n even though HeidiSQL is only showing \r\t, but why PHP can't determine how many backslashes are there? How I can remove that one extra backslash? I even tried with regex replace but it didn't work.
2023-01-23 23:01:07
@Barmar I've solved it after a couple of hours on this by using stripcslashes