hi I need to read a text file and then explode it to two piece then match it with give name then dispay if it matches...so far the code I made is given below..but it doesnt work..can any one tell me what wrong is with this code??
$name = "thomas";
$filename = file("land.txt");
//$contents = fread($handle, filesize($filename));
for($i=0; $i<count($filename); $i++)
{
$string = explode(":", $filename[$i]);
if($name == $string[1])
$id = $string[0];
}
echo $id;
this case it should display "D1"; but it doesnt!!
content of "land.txt"
file
D1:thomas
D6:benny
D7:alwyn
D25:mathew
D9:peter
Try
$filename = file("land.txt", FILE_IGNORE_NEW_LINES);
Or
$string = explode(":", trim($filename[$i]));
What is FILE_IGNORE_NEW_LINES
Over here
if($name == $string[1]) use if($name == trim($string[1]))
.
Try this and let me know.
$name = 'thomas'; $content = file('land.txt'); $id = NULL; foreach($content as $no => $line){ $array = explode(':', $line); if($array[1] == $name) $id = $array[0]; } echo $id;