Friday, January 7, 2011

php - working with function (2)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
 <title>Untitled</title>
</head>
<body>


<?php

//
// accept a list of email address,
// split address, add domain part to another array
//

function getUniqueDomains($list){
 $domains=array();
 foreach($list as $l){
  //
  // separate
xxx@yyy, and save arr[0]=xxx, arr[1]=yyy
  //

  $arr=explode("@", $l);
  //
  // save yyy into domain[]
  //

  $domains[]=trim($arr[1]); 
 }
 //
 // array_unique() removes duplicates elements
 //

 return array_unique($domains);
}


// read the file with email list
$fileContents=file("data.txt");

// pass the deliminated array
$returnedArray=getUniqueDomains($fileContents);

// print returned array
foreach($returnedArray as $d){
 print "$d<br />";
}


?>

</body>
</html>

No comments:

Post a Comment