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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,