|
Posted by Andy Jeffries on 03/29/06 11:28
On Tue, 28 Mar 2006 18:57:53 -0800, Mark wrote:
> Hello all,
>
> I have the following file.txt storing these kind of values:
>
> Name: John Doe
> E-mail: john@doe.com
> Phone: 555 865 8901
> Address: 71 Huntington Ln. (33444) FL - Delray Beach US Member ID:
> 373732510149005
> Registration date: 5/2005
> Access code: 6691
> Comments: Thanks, great community
> ============================================ Name: Bill Gates
> E-mail: billgates@owns.microsoft.com
> Phone: (222) 345-6667
> Address: P.O. Box 36 (73456) OK - Leedey USA Member ID: 5178052509437626
> Registration date: 10/2005
> Access Code: 969
> Comments: Reffered by Josh Portua. Give him credit
> ============================================ Name: ...... and so on
>
> I want to search the file.txt after 3 predefined Member ID prefixes:
> $prefix1="51780";
> $prefix2="37373";
> $prefix3="72215";
> show the Member IDs that match this rule and also display for the found
> matching Member IDs their E-mail and Name.
OK, new script follows:
<?php
$prefix1="51780";
$prefix2="37373";
$prefix3="72215";
$userfile = "users.txt";
$fc=file($userfile);
foreach($fc as $line_num => $line)
{
$line = trim($line); // Line will have newline characters
if (ereg("^E-mail: (.+)", $line, $matches)) {
$email = $matches[1];
}
if (ereg("^Name: (.+)", $line, $matches)) {
$name = $matches[1];
}
if (strstr($line,$prefix1)) {
print "Found ID $prefix1\nName: $name\nEmail: $email\n\n";
}
elseif (strstr($line,$prefix2)) {
print "Found ID $prefix2\nName: $name\nEmail: $email\n\n";
}
elseif (strstr($line,$prefix3)) {
print "Found ID $prefix2\nName: $name\nEmail: $email\n\n";
}
}
?>
This assumes, as per your data that the Name and Email address for a given
member appears before their membership ID.
If not, you'd need to store the ID in the same was as the ereg statements
for name and email and react on reaching the line with all the equals
statements, then see if the ID matches.
Cheers,
--
Andy Jeffries MBCS CITP ZCE | gPHPEdit Lead Developer
http://www.gphpedit.org | PHP editor for Gnome 2
http://www.andyjeffries.co.uk | Personal site and photos
Navigation:
[Reply to this message]
|