Php 在数组中分隔字符串&;将其分配给2个变量

Php 在数组中分隔字符串&;将其分配给2个变量,php,arrays,string,Php,Arrays,String,好吧,所以我把邮件列表弄得一团糟,我陷入了精神僵局。以下是我所拥有的: mailing_list.php//以“Name:Email”的形式包含所有详细信息 functions.php//包含函数get\u registered\u list,该函数应在页面上显示该列表 function get_registered_list($path = 'mailing.php') { $content = file($path); // creates an array of strings of t

好吧,所以我把邮件列表弄得一团糟,我陷入了精神僵局。以下是我所拥有的:

  • mailing_list.php//以“Name:Email”的形式包含所有详细信息
  • functions.php//包含函数get\u registered\u list,该函数应在页面上显示该列表

    function get_registered_list($path = 'mailing.php') {
    $content = file($path); // creates an array of strings of the format "Name:Email"
    // enter code here
    }
    
您将如何做到这一点:

  • 运行数组$content并将每个字符串作为名称和电子邮件分开
非常感谢您的帮助


谢谢

请尝试以下方式获取内容:

$handle = fopen($path, "r")
$content = fgetcsv($handle, 0, ':');
fclose($handle);
或者简单地说:

$content = file_get_contents($path);
$array = str_getcsv($content, ':');
您还可以在
foreach
循环中解析数组,但不建议这样做,因为您必须处理输入文件中的任何特殊字符:

foreach ( $content as $item ) {
    list($name, $email) = explode(':', $item);
    echo "{$name} ({$email})" . PHP_EOL;
}