PHP 字符串操作
我正在读取格式为
Phone#(tab)Text(换行符) 的文本文件 (换行)
或者更具体地说,
4799999999 Hey! You owe us $576.53. Pay up to account no. 9760.00.12345, ID: 201561438.
4798888888 Hey! You owe us $199. Pay up to account no. 9760.05.12345, KID: 201565173.
4797777777 Hey! You owe us... and so on.
我想将其存储在 SQL 数据库中,因此我需要将其分成电话号码和文本。
关于如何在 PHP 中将字符串分成对有什么想法吗?我应该在保存到 SQL 之前将这些值存储在数组中,还是应该立即存储?
I'm reading a textfile on the format
Phone#(tab)Text(line break)
(line break)
Or more specifically
4799999999 Hey! You owe us $576.53. Pay up to account no. 9760.00.12345, ID: 201561438.
4798888888 Hey! You owe us $199. Pay up to account no. 9760.05.12345, KID: 201565173.
4797777777 Hey! You owe us... and so on.
I want to store this in a SQL database, so I need to break this apart in phone numbers and text.
Any ideas on how to split the string into pairs in PHP? And should I store these values in a Array before saving to SQL, or should I just store it right away?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
逐行读取文件,在选项卡上拆分每一行,然后使用适合您的数据库平台的 SQL 将其放入数据库中。
您哪个部分遇到问题?
Read the file line by line, split each line on the tab car, and then put it into the DB using suitable SQL for your DB platform.
Which part are you having trouble with?
使用带有选项卡的
explode
函数每行上的 (\t
) 分隔符。Use the
explode
function with a tab (\t
) delimiter on each line.您可以使用正则表达式来分解字符串,然后使用 SQL 将它们输入到数据库中。
将返回电话号码作为第一个捕获组,并将剩余文本作为第二个捕获组。
You could use Regular Expressions to break up the strings and then input them into the database using SQL.
would bring back the Phone Number as the first captured group, and the remaining text as the second captured group.
假设文件不大,您可以使用 将行读入数组中
file()
并像这样迭代它们:Assuming the file isn't huge, you can just read the lines into an array with
file()
and iterate over them like so:我认为您最好使用
strpos
函数,以防文本部分中有选项卡。将
file
函数和foreach
替换为fopen
和fgets
(查看示例)如果文件特别大。I think you'd be better off using the
strpos
function, in case there are tabs in the text part.Replace the
file
function andforeach
withfopen
andfgets
(see example) if the file is particularly large.