Doug deloy: some Sample php code and apps

[HOME] [Form Example] [Session Example] [Email Validation ] [Regular Expressions ][deloycom home

Searching for a Phone from a File
Using regular expressions to search for a phone no from a file

Please enter the area code


Here's the code for the example

$filename="$_SERVER[DOCUMENT_ROOT]/datebook";
$lines = file($filename);
$area_code=trim($_POST['area_code']);
echo "

Names and Phones in $area_code area code";
foreach ($lines as $the_line) {
$fields=explode(":",$the_line);
$name=$fields[0];
$phone=$fields[1];
if( preg_match("/^$area_code-/","$phone")){
echo "$name: $phone
";
$count++;
}
}
if ($count == 0 ){
echo "The area code is not found.
";
}