473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

file() and line_num display

Hello all,

I have the following file.txt storing these kind of values:

Name: John Doe
E-mail: jo**@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: bi*******@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.

So the search on the part of file.txt shown at the start will result:
Member ID: 5178052509437626 E-mail: bi*******@owns.microsoft.com Name:
Bill Gates

I have built something like this so far:

<?php
$prefix1="51780";
$prefix2="37373";
$prefix3="72215";
$userfile = "users.txt";
$fc=file($userfile);
foreach($fc as $line_num => $line)
{
if (strstr($line,$prefix1))
echo "Found one match at {$line_num} $line<br>";
if (strstr($line,$prefix2))
echo "Found one match at {$line_num} $line<br>";
if (strstr($line,$prefix3))
echo "Found one match at {$line_num} $line<br>";
}
?>

What I can't do is showing the emails and names for the found member
ids. Please, can you give me any suggestions? I'm completly blank.

Thank you for your time.

Regards,
Mark

Mar 29 '06 #1
6 2290
Mark wrote:
Hello all,

I have the following file.txt storing these kind of values:

Name: John Doe
E-mail: jo**@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: bi*******@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.

So the search on the part of file.txt shown at the start will result:
Member ID: 5178052509437626 E-mail: bi*******@owns.microsoft.com Name:
Bill Gates

I have built something like this so far:

<?php
$prefix1="51780";
$prefix2="37373";
$prefix3="72215";
$userfile = "users.txt";
$fc=file($userfile);
foreach($fc as $line_num => $line)
{
if (strstr($line,$prefix1))
echo "Found one match at {$line_num} $line<br>";
if (strstr($line,$prefix2))
echo "Found one match at {$line_num} $line<br>";
if (strstr($line,$prefix3))
echo "Found one match at {$line_num} $line<br>";
}
?>

What I can't do is showing the emails and names for the found member
ids. Please, can you give me any suggestions? I'm completly blank.

Thank you for your time.

Regards,
Mark

Mark,

I'd use a database such as MySql or Postgres. They are ideally suited for this
kind of operation. And it's not that hard to program for them.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 29 '06 #2
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: jo**@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: bi*******@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

Mar 29 '06 #3
On Tue, 28 Mar 2006 22:34:08 -0500, Jerry Stuckle wrote:
I'd use a database such as MySql or Postgres. They are ideally suited for
this kind of operation. And it's not that hard to program for them.


I'd agree if he is the one inserting them in to a text file (he may be
being given it by another party).

If he only has the text file, he'd have to parse it either way to insert
it into a DB or to just display it - so he might as well just display it.

Cheers,
Andy

--
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

Mar 29 '06 #4
Andy Jeffries wrote:
On Tue, 28 Mar 2006 22:34:08 -0500, Jerry Stuckle wrote:
I'd use a database such as MySql or Postgres. They are ideally suited for
this kind of operation. And it's not that hard to program for them.

I'd agree if he is the one inserting them in to a text file (he may be
being given it by another party).

If he only has the text file, he'd have to parse it either way to insert
it into a DB or to just display it - so he might as well just display it.

Cheers,
Andy


Yes, he would have to parse it *once* to place all the information in the
database. As a flat file he would have to parse it every time he wants anything
from the database. And if the file format ever changes, he'll have to redo the
code every place he needs to access it.

Databases make your life so much simpler - even if you're not inserting into a
file. Even with static data I almost always put the information in a database;
it's just so much easier to access - and you don't run into problems like this.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Mar 29 '06 #5
On Wed, 29 Mar 2006 05:36:04 -0500, Jerry Stuckle wrote:
I'd use a database such as MySql or Postgres. They are ideally suited
for this kind of operation. And it's not that hard to program for them.
I'd agree if he is the one inserting them in to a text file (he may be
being given it by another party).

If he only has the text file, he'd have to parse it either way to insert
it into a DB or to just display it - so he might as well just display
it.


Yes, he would have to parse it *once* to place all the information in the
database. As a flat file he would have to parse it every time he wants
anything from the database. And if the file format ever changes, he'll
have to redo the code every place he needs to access it.


It depends what he's using the data for. Maybe this is code to parse it
put in to a database? I know at the moment he only wants to print it, but
maybe he's just taking it one step at a time.

To be honest, I think we actually have the same point of view on this, I
completely agree with you - I was just trying to explain (more for the
OP's benefit as I know you're a knowledgeable guy) that a database is a
great end-goal, but if it's for a one-off run it might be easier to just
display the information rather than add an intermediate step.

Your answer to "how do I display these fields" was "use a database"
without the intermediate step of how to get his data from the text file he
has in to the empty database he could make.
Databases make your life so much simpler - even if you're not inserting
into a file. Even with static data I almost always put the information
in a database; it's just so much easier to access - and you don't run
into problems like this.


Again though, while I generally agree - it depends on the purpose.
Without more information from the OP it's difficult for you or I to advise
whether putting it in a database is the best course of action.

For example, this may be a much simplified version of what he wants and it
may just be that his boss has said "here's a one-off dump of the CRM
database, can you get me the details for these users". He's
therefore writing a script to do it (wise man, the boss may ask the same
thing next month) but creating a database would be overkill as it won't be
accessed until possibly next month and by then the data would need
re-importing anyway.

But as I said, without knowing we're both just guessing :-)

Cheers,
Andy

--
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

Mar 29 '06 #6
I would like to give a heartly "Thank You!" to Andy Jeffries for
helping me out on this. The script was just what I needed. And you made
a really good point in your replies. I could have used a database but
that was just too much work for something that won't be used intense.
Those dumps appear from time to time and it's no point in having them
stored in a database just for one time use.

Again, Thank you Andy Jeffries for all your help, for your flawless
script and for giving me priceless directions. I was so blanked-out
before and couldn't have imagined a way to solve this.

Best regards,
Mark

Mar 30 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: VooDoo | last post by:
Hi, I have a Temperature sensor with internal web server that provide the data in a text format. When quering the ip(ttp://192.168.10.220/temp) of the sensor i get that kind of reply: Probe...
10
by: Alex Hopson | last post by:
I'm trying to read an html file from my local server into a string, I'm using the following code: $attfile = $attachment; //create filenames $file_name = basename ($attfile); $lines =...
11
by: Skc | last post by:
I have a .txt which has been exported as a .csv from an external source. What i need to do is to import this into SQL2000 (into a table) but I need to do special things on the data: 1. I need to...
4
by: anne001 | last post by:
Hi For a class, students are going to run an experiment on line. Each time a subject runs, his/her data is appended to one giant text file. Their own data set will be just one line starting with...
1
by: bbepristis | last post by:
Hey all I have a wired issue I have a csv file with , seperated values I have some code to read the file and put the record into an array then into textboxes. the problem is it seems to read...
1
by: pukya78 | last post by:
Hi, I am trying to get the current file and the line number which is getting executed. I used: MessageBox.Show(New StackTrace(New StackFrame(True)).GetFrame(0).GetFileLineNumber) which gives me...
3
by: walterbyrd | last post by:
I know I can use the file() function to read a file into an array by providing a literal file name between single quotes: $lines = file('literal_file_name'); But, what if I want file() to use...
15
by: Sullivan WxPyQtKinter | last post by:
I have a huge log file which contains 3,453,299,000 lines with different lengths. It is not possible to calculate the absolute position of the beginning of the one billionth line. Are there...
2
by: Andrew West | last post by:
Probably a bit of weird question. I realise decorators shouldn't be executed until the function they are defined with are called, but is there anyway for me to find all the decorates declared in a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.