如何取出每行的前5个字符?
我有一个文本文件,在文件中我
22222 hihihi
33333 hihihi
kjhkh hihihi
想编写一个应用程序来与 mysql 数据库进行比较,这样如果数据库中的 ID
与文本文件中的第一列相同,则它将通过用文本文件中第二列中的信息替换名称来更新数据。
我该怎么做?
I have a text file, inside the file I have
22222 hihihi
33333 hihihi
kjhkh hihihi
I want to write an application to compare with mysql database so that if my ID
in the database is the same as the first column in the text file, it will update the data by replacing the name with the information in the second column in the text file.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这似乎是一个分隔文本文件。您可以使用文本驱动程序非常轻松地将数据加载到记录集中。我不喜欢 PHP,但我相信 PHP 有文本驱动程序。如果您的应用程序在 Windows 上运行,则 Microsoft Jet 支持可用于将数据放入记录集的文本驱动程序。
This seems to be a delimited text file. You can use a text driver to load the data into a record-set very easily. I am not into PHP, but I believe text-drivers are there for PHP. If your app runs on Windows, Microsoft Jet supports text driver that can be used to get the data into a record-set.
正如您上面提到的,字段之间用制表符分隔,您可以将文件直接导入MySQL(假设id是该表的主键):(
注意:这不仅会更新,还会添加条目)
As you mentioned above that the fields are separated by a tab, you can import the file directly into MySQL (assuming id is the primary key of that table):
(Note: this will not only update but also add entries)
您可以尝试将数据加载到临时表中,运行更新,然后删除临时表 - 类似于:(
用 DATA 替换 DXTX,用 TABLE 替换 TXBLE - 就是上面的方式,因为我的工作有一个过滤系统)
请参阅< a href="http://dev.mysql.com/doc/refman/4.1/en/load-data.html" rel="nofollow">http://dev.mysql.com/doc/refman/4.1/ en/load-data.html 了解有关加载数据的更多信息。
主要在 PHP 中处理这个问题可能会比较慢,但如果你需要走这条路,那么它会是这样的:
上述代码的潜在陷阱:
请参阅 http:// php.net/manual/function.explode.php 和 mysql-query 文档也有更多信息
You could try loading the data into a temporary table, running the update, then dropping the temp table - something like:
(replace DXTX with DATA and TXBLE with TABLE - it's that way above because my work has a filtering system in place)
See http://dev.mysql.com/doc/refman/4.1/en/load-data.html for more info on LOAD DATA.
Handling this primarily in PHP would likely be slower, but if you need to go that route then it would be something like:
Potential gotchas for the above code:
See http://php.net/manual/function.explode.php and the mysql-query docs there as well for more info
如果您确定它始终是 5 个字符,您可以简单地使用:
If you're sure it's always 5 characters, you could simply use:
您可以使用
explode()
将字符串拆分为行;然后,对于每一行,一个简单的正则表达式,其中preg_match()
功能,以匹配您感兴趣的部分。例如,这样的事情:
会让你:
关于我使用的正则表达式的注释:
^
[^\s]
[^\s]+
([^\s]+)
\s
\s+
.*
(.*)
$
现在,当您使用文件而不是字符串作为输入时,您可能希望逐行读取文件,而不是将整个文件提取到内存中;为此,请参阅
fgets()
函数 - 有一个示例它的手册页。Your could use
explode()
to split your string into lines ; and, then, for each line, a simple Regular expression, with thepreg_match()
function, to match the parts that interest you.For example, something like this :
Would get you :
Notes about the regex I used :
^
[^\s]
[^\s]+
([^\s]+)
\s
\s+
.*
(.*)
$
Now, as you are working with a file as input, and not a string, you might want to read the file line by line, not fetching the whole file into memory ; for that, see the
fgets()
function -- there is an example on its manual page.