用换行符爆炸字符串的奇怪问题
我不知道这段 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要灵活掌握
换行
的外观。您可以进行灵活的分解:...或者您可以将换行规范化为您选择的格式:
还值得注意的是,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:... or you can normalize line feeds to the format of your choice:
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 theSHOW CREATE TABLE
output is often not necessary:http://dev.mysql.com/doc/refman/5.1/en/show.html
所有页面都是在同一台计算机上创建的吗?不同操作系统的行结束字符代码不同。可以是以下之一:
\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.