Using PHP, I want to generate custom MySQL dump file (due to cannot use exec and a few other reasons).
I found these 2 similar solutions:
- https://www.kvcodes.com/2017/10/php-create-mysql-backup
- https://davidwalsh.name/backup-mysql-database-php
The different part that I want to highlight is:
Link 1:
$row[$j] = preg_replace("#\n#", "\\n", $row[$j])
Link 2:
$row[$j] = ereg_replace("\n","\\n",$row[$j])
I know that ereg_replace() is deprecated. I also assume that there's typo in solution Link 1 which should be:
$row[$j] = preg_replace("\n", "\\n", $row[$j])
But then in my solution, I simply use:
$row[$j] = str_replace("\n", "\\n", $row[$j])
How could I be wrong by using str_replace()? I've tested dumping several complex data (json, coding, html syntax) and all seems ok. Could there some special case that str_replace() would handle differently than preg_replace()?