PHP 字符串操作

发布于 2024-08-14 12:18:43 字数 429 浏览 5 评论 0原文

我正在读取格式为

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 技术交流群。

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

发布评论

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

评论(6

作业与我同在 2024-08-21 12:18:43

逐行读取文件,在选项卡上拆分每一行,然后使用适合您的数据库平台的 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?

毅然前行 2024-08-21 12:18:43
$data = file_get_contents ($file); // Assuming $file holds path to file
if ($data) {
    $lines = explode("\n\n", $data);
    foreach ($lines as $line) {
        list ($phone, $message) = explode("\t", $line, 2);
        // do whatever you need with it
    }
}
$data = file_get_contents ($file); // Assuming $file holds path to file
if ($data) {
    $lines = explode("\n\n", $data);
    foreach ($lines as $line) {
        list ($phone, $message) = explode("\t", $line, 2);
        // do whatever you need with it
    }
}
动次打次papapa 2024-08-21 12:18:43

使用带有选项卡的 explode 函数每行上的 (\t) 分隔符。

Use the explode function with a tab (\t) delimiter on each line.

喵星人汪星人 2024-08-21 12:18:43

您可以使用正则表达式来分解字符串,然后使用 SQL 将它们输入到数据库中。

^([0-9]{10})(.*)

将返回电话号码作为第一个捕获组,并将剩余文本作为第二个捕获组。

You could use Regular Expressions to break up the strings and then input them into the database using SQL.

^([0-9]{10})(.*)

would bring back the Phone Number as the first captured group, and the remaining text as the second captured group.

抱着落日 2024-08-21 12:18:43

假设文件不大,您可以使用 将行读入数组中file() 并像这样迭代它们:

$lines = file($filename);

foreach($lines as $line) {
  $line_arr = explode("\t", $line, 2);

  if(!empty($line_arr)) {
    // $line_arr[0] will be the phone number and
    // $line_arr[1] will be the text; insert them into your database
    // however you please.
  }
}

Assuming the file isn't huge, you can just read the lines into an array with file() and iterate over them like so:

$lines = file($filename);

foreach($lines as $line) {
  $line_arr = explode("\t", $line, 2);

  if(!empty($line_arr)) {
    // $line_arr[0] will be the phone number and
    // $line_arr[1] will be the text; insert them into your database
    // however you please.
  }
}
执笏见 2024-08-21 12:18:43

我认为您最好使用 strpos 函数,以防文本部分中有选项卡。

$lines = file( $filename );

foreach ( $lines as $line )
{
    $tab = strpos( $line, "\t" );
    $num = substr( $line, 0, $tab );
    $text = substr( $line, $tab );
}

file 函数和 foreach 替换为 fopenfgets (查看示例)如果文件特别大。

I think you'd be better off using the strpos function, in case there are tabs in the text part.

$lines = file( $filename );

foreach ( $lines as $line )
{
    $tab = strpos( $line, "\t" );
    $num = substr( $line, 0, $tab );
    $text = substr( $line, $tab );
}

Replace the file function and foreach with fopen and fgets (see example) if the file is particularly large.

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