2013-11-24 127 views
0

好的,所以我搞乱了电子邮件列表,我陷入了精神僵局的状态。这里是我有:将数组中的字符串分开并将其分配给2个变量

  • mailing_list.php //包含在表单中的所有细节“姓名:电子邮件”
  • 的functions.php //包含功能get_registered_list,这应该显示在列表中页。

    function get_registered_list($path = 'mailing.php') { 
    $content = file($path); // creates an array of strings of the format "Name:Email" 
    // enter code here 
    } 
    

你将如何做到这一点:

  • 运行通过数组$内容和每个字符串姓名和电子邮件分开。

帮助将不胜感激。

感谢

回答

0

尝试让内容与fgetcsv()这样的:

$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; 
} 
相关问题