PHP将反斜杠添加到file_get_contents

I have a large sql file that was generate from sql studio that creates a database and tables within it. I'm using php's get_file_contents and going line by line to see if the line includes CREATE DATABASE to parse out the database name. The problem is that php is adding backslashes before every character and i'm not sure how to stop that.

USE [master]
GO
CREATE DATABASE [FOOBAR-DEV]
 CONTAINMENT = NONE
GO

// and so on for another 10,000 lines
$db = "FOOBAR-DEV";
$path = sprintf('%s/../../sql/%s/createdb.sql', __DIR__, $db);
$contents = file_get_contents($path);
$lines = explode("\n", $contents);
$name = null;
foreach ($lines as $key => $line) {
    $needle = 'CREATE DATABASE';
    // debug code
    if ($key <= 10) {
        $output->writeln($line);
    }
    if (strpos($line, $needle) !== false) {
        $output->writeln('found');
        $name = substr($line, strlen($needle));
        break;
    }
}
$output->writeln($name);

The line is never found because i'm getting this: enter image description here

I've tried stripslashes on the $line but that doesn't seem to do anything. I don't believe i have magic quotes on. Anything with "magic" from php -i just shows Imagick settings.

How can I remove the backslashes in the $line?

评论
  • Guy
    Guy 回复

    我不确定如何解决php本身的问题,但是我能够在PhpStorm中将文件编码转换为utf-8,这似乎可以解决问题。