用换行符爆炸字符串的奇怪问题

发布于 2024-10-10 22:48:27 字数 1028 浏览 7 评论 0原文

我不知道这段 PHP 代码有什么问题:

$sql = "CREATE TABLE test (
 id mediumint(9) unsigned NOT NULL auto_increment,
 filenames text NOT NULL,
 meta longtext,
 added_date datetime NOT NULL default '0000-00-00 00:00:00',
 PRIMARY KEY  (id)
)";
var_export(explode("\n",$sql));

上面的代码基本上用换行符('\n')分解 $sql 中的字符串,然后使用 var_export 输出它。我有一些 PHP 测试文件来处理代码,但并非所有文件都显示我希望的内容:

array ( 0 => 'CREATE TABLE test ( ', 
        1 => ' id mediumint(9) unsigned NOT NULL auto_increment, ', 
        2 => ' filenames text NOT NULL, ', 
        3 => ' meta longtext, ', 
        4 => ' added_date datetime NOT NULL default \'0000-00-00 00:00:00\', ', 
        5 => ' PRIMARY KEY (id) ', 6 => ') ;', 
)

有些显示此文件:

array ( 0 => 'CREATE TABLE test ( id mediumint(9) unsigned NOT NULL auto_increment, filenames text NOT NULL, meta longtext, added_date datetime NOT NULL default \'0000-00-00 00:00:00\', PRIMARY KEY (id) ) ;', )

注意到区别了吗?第二个显示它,因为没有换行符来分解字符串。我不明白这里发生了什么。也许有人知道一些事情吗?

I dont know what's wrong with this PHP code:

$sql = "CREATE TABLE test (
 id mediumint(9) unsigned NOT NULL auto_increment,
 filenames text NOT NULL,
 meta longtext,
 added_date datetime NOT NULL default '0000-00-00 00:00:00',
 PRIMARY KEY  (id)
)";
var_export(explode("\n",$sql));

The code above basically explode the string in $sql with newline char ('\n') and then output it using var_export. I have some PHP tests file to worked with the code but not all of them displaying what I hope it would be:

array ( 0 => 'CREATE TABLE test ( ', 
        1 => ' id mediumint(9) unsigned NOT NULL auto_increment, ', 
        2 => ' filenames text NOT NULL, ', 
        3 => ' meta longtext, ', 
        4 => ' added_date datetime NOT NULL default \'0000-00-00 00:00:00\', ', 
        5 => ' PRIMARY KEY (id) ', 6 => ') ;', 
)

some diplaying this instead:

array ( 0 => 'CREATE TABLE test ( id mediumint(9) unsigned NOT NULL auto_increment, filenames text NOT NULL, meta longtext, added_date datetime NOT NULL default \'0000-00-00 00:00:00\', PRIMARY KEY (id) ) ;', )

notice the difference? the second one displaying it as there are no newline to exploding the string. I don't get what's happening here. Anyone knows something maybe?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

御弟哥哥 2024-10-17 22:48:27

您需要灵活掌握换行的外观。您可以进行灵活的分解:

preg_split("/[\r\n]+/", $sql)

...或者您可以将换行规范化为您选择的格式:

$sql = strtr($sql, array(
    "\r\n" => "\n",
    "\r" => "\n",
));
explode("\n",$sql)

还值得注意的是,MySQL 提供了多个 SHOW 语句,允许您获取最有用的数据片段来自表定义。解析 SHOW CREATE TABLE 输出通常是不必要的:

http://dev.mysql.com/doc/refman/5.1/en/show.html

You need to be flexible with what a line feed looks like. You can make a flexible explode:

preg_split("/[\r\n]+/", $sql)

... or you can normalize line feeds to the format of your choice:

$sql = strtr($sql, array(
    "\r\n" => "\n",
    "\r" => "\n",
));
explode("\n",$sql)

It's also worth noting that MySQL offers several SHOW statements that allow you to fetch most useful piece of data from table definitions. Parsing the SHOW CREATE TABLE output is often not necessary:

http://dev.mysql.com/doc/refman/5.1/en/show.html

紫竹語嫣☆ 2024-10-17 22:48:27

所有页面都是在同一台计算机上创建的吗?不同操作系统的行结束字符代码不同。可以是以下之一:\n\r\r\n,具体取决于系统是 Windows、Mac ,或*nix。您可能需要检查所有三个。

Were all pages created on the same computer? The line ending character code is different for different operating systems. It can be one of the following: \n, \r, and \r\n, depending on whether the system is Windows, Mac, or *nix. You may want to check for all three.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文