473,770 Members | 2,120 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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: 517805250943762 6
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: 517805250943762 6 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($userf ile);
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 2305
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: 517805250943762 6
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: 517805250943762 6 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($userf ile);
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*******@attgl obal.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: 517805250943762 6
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($userf ile);
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*******@attgl obal.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
2724
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 1|82.2|Probe 2|80.8|Probe 3|-99.9|Probe 4|-99.9 I woud like to import the result into my mysql database. What is the best way to do this? Should i use a fopen() ? I have try with this but i don't know how get just the value data....
10
2509
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 = file($attfile); //get file into array foreach ($lines as $line_num => $line) { //concatenate each line $fcontent.= $line; }
11
3093
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 look for the first three chars and import rows into separate tables. E.g. if the first three chars begin with CCC, then this row goes into the CCC_table, if it is TTT then into the TTT_table etc... 2. Once I have my tables built up, I need to do...
4
10635
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 the keyword they gave as identification. The faculty does not want the students to be able to download and see the giant data file. He wants the students to only download and see the data that starts with their own identification tag.
1
1547
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 everyother line of the file not everyline at first I thought it might be the code then the csv file The csv is a mysql generated via webmin however I cant seem to find the problem anywhere so please advise. -- CODE-- Public Sub csv_conn() 'Opens...
1
1873
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 the line number. Later on, I was trying to write a generalized routine, so that I can log the file name and the method.
3
1775
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 a variable name? $variable_file_name = "literal_file_name"; $lines = file($variable_file_name);
15
2075
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 efficient way to seek to the beginning of that line in python? This program: for i in range(1000000000): f.readline() is absolutely every slow....
2
1953
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 file when I import it? Or perhaps anyway to find the decorators by loading the file by other methods (with out simply parsing it by hand). Basically what I'm looking for is a way to, given a python file, look through that file and find all the...
0
9618
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6712
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.