duanoucuo7045 2016-12-11 17:53
浏览 16
已采纳

PHP拆分多个字符串

I have a text file: 'bookdata' with the following structure:

abcd@yahoo.com:1:20.30
efgh@hotmail.com:4:5.05
 ...

email : total no. of books : total books read.

My problem is to separate each part of the various strings using an associative array as follows:

 +-----------------------------------------------+
 |                     yahoo.com                 |
 +---------------+------------------+------------+
 | abcd@yahoo.com |         1        |     20.30 |

 | efgh@yahoo.com |         4        |      5.05 |
 +---------------+------------------+------------+

My approach so far is as follows:

I have a function makeArray() that has the data from the read txt file:

public function makeArray()
{
    $readTxtData = $this->read('bookdata');
    //Get the domain from data....
    $domain = preg_split("~[A-Za-z](.*?)@~", $readTxtData);

    return $domain;
}

The result:

        Array
    (
        [0] => 
        [1] => yahoo.com:7:8.35

        [2] => hotmail.com:4:5.59
)

The array should look like this:

 Array
(
    [yahoo.com]
        (
            [0]
                (
                    [0] => abcd@yahoo.com
                    [1] => 7
                    [2] => 8.35
                )

            [1] => Array
                (
                    [0] => efgh@yahoo.com
                    [1] => 1
                    [2] => 8.36
                )

            [2] => Array
                (
                    [0] => oyp@yahoo.com
                    [1] => 9
                    [2] => 13.42
                )
        ).....

Thanks.

  • 写回答

1条回答 默认 最新

  • drq231358 2016-12-11 18:14
    关注

    Assuming the fact that you're reading the entire text file data in $readTxtData variable, the solution to your problem would be like this,

    public function makeArray(){
        $readTxtData = $this->read('bookdata');
    
        $lines = explode("
    ", $readTxtData);
        $result = array();
        foreach($lines as $line){
            $components = explode(":", $line); 
            $domain = explode("@", $components[0]);
            $result[$domain[1]][] = $components;
        }
        return $result;
    }
    

    Algorithm:

    1. Create an empty array named $result.

      $result = array();
      
    2. Loop through each of the lines of text file i.e. $lines using foreach loop and do step 3 to 5.

      foreach($lines as $line){
          ...
      }
      
    3. Explode each line i.e. $line to get all three components, such as email, total no. of books and total books read in an array i.e. $components, like this:

      Array
      (
          [0] => abcd@yahoo.com
          [1] => 1
          [2] => 20.30
      )
      

      This step is performed by the following statement,

      $components = explode(":", $line);
      
    4. Explode $components[0] string to get all domain related components, such as user name and domain name in an array i.e. $domain, like this:

      Array
      (
          [0] => abcd
          [1] => yahoo.com
      )
      

      This step is performed by the following statement,

      $domain = explode("@", $components[0]);
      
    5. Now use the domain name i.e. yahoo.com as key in $result array and append $components array to it accordingly.

      $result[$domain[1]][] = $components;
      
    6. Finally return the $result array.

      return $result;
      

    Here are the necessary references:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办