Find something in a file and get the line number?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Find something in a file and get the line number?

Post by toasty2 »

When I find a key word in a file, is there a way i can get the line number that it was found on?

I'm searching an array produced by file(). Then I need to get the line number (array key) that the word was found on.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: Find something in a file and get the line number?

Post by volka »

GWsux wrote:I'm searching an array produced by file().
Exactly how?
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Well, I'm not sure exactly how I should do it...since its an array.

Here's the file I wish to get data out of: http://server.randomresources.com/statu ... report.txt

It's a CPU-Z report on hardware. For example, I want to get the processor name, so I search for "Specification" in the file, then I separate Specification from whats after it and use whats after it. Here's what I have so far, the only problem is that the line numbers could be dynamic, so this won't work on all systems:

Code: Select all

$cpuz_report = file('tools\report.txt');
// Processor
$cpu = explode("Specification",$cpuz_report[23]); // In this example I know the line number to get the info from.
$cpu = trim($cpu[1]); // processor name
On my server machine, $cpu's end value is "AMD Athlon(tm) XP 2000+", so that works...so, I know what to do once I know the line number, but on different machines the line number could change, which is why searching for "Specification" instead and getting it's line number/array key # is necessary.
Last edited by toasty2 on Wed Apr 11, 2007 7:25 am, edited 1 time in total.
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

array_search() should work
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Thanks, but I have a problem. In the following code, its not getting the array key, the variable $cpu_k is empty. Here's the code:

Code: Select all

$cpuz_report = file('tools\report.txt'); // cpuz report

// Processor
$cpu_k = array_search('Specification', $cpuz_report);
echo $cpu_k; // Outputs nothing!?
$cpu = explode("Specification",$cpuz_report[$cpu_k]);
$cpu = trim($cpu[1]); // processor name
So, for some reason its not getting the array key and storing it in the variable $cpu_k. I have confirmed that it is reading the reports.txt, and that "Specification" is inside the file, because if I manually specify the line number, the explode() in my code works flawlessly and outputs the info. What's wrong?
Last edited by toasty2 on Wed Apr 11, 2007 5:25 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The value must match (entirely, case sensitive too) for array_search() to find a match. If you need partial value matching then you need to do the search yourself.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Can't you do like a foreach() loop on the file array and check stristr() for each line? It is slow, but could do what you want it to.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

feyd wrote:The value must match (entirely, case sensitive too) for array_search() to find a match. If you need partial value matching then you need to do the search yourself.
But, its not partial what I'm searching for. What I'm searching for IS in the file, so I don't understand why it isn't finding it. I am searching for "Specification" in this file: http://server.randomresources.com/statu ... report.txt
Its around line 20 and I'm sure it matches completely with what I am searching.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Why not this?

Code: Select all

<?php
$file = 'file-opening-test-data.txt';
$search = 'Specification';

$f = array();
$line_num = -1;

if (file_exists($file))
{
	$f = file($file);
	
	foreach ($f as $num => $line)
	{
		if ( (stripos($line, $search)) !== false )
		{
			$line_num = $num;
			break;
		}
		
	}
}

if ($line_num >= 0)
{
	echo $search . ' was found on line ' . $line_num . ' and contains the following information:<br />';
	echo '<strong>' . $f[$line_num] . '</strong>';
}

//echo '<pre>'; var_dump($f); echo '</pre>';
?>
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Thanks, it works. And I turned it into an easy to use function :D
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Cool.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

GWsux wrote:
feyd wrote:The value must match (entirely, case sensitive too) for array_search() to find a match. If you need partial value matching then you need to do the search yourself.
But, its not partial what I'm searching for. What I'm searching for IS in the file, so I don't understand why it isn't finding it. I am searching for "Specification" in this file: http://server.randomresources.com/statu ... report.txt
Its around line 20 and I'm sure it matches completely with what I am searching.
Just to be clear on why array_since() isn't working as you expected, the array $cpuz_report had a row with the value of

Code: Select all

Specification		AMD Athlon(tm) XP 2000+
While you were searching for simply "Specification". As feyd already explained, array_search() won't match partial values.
toasty2
Forum Contributor
Posts: 361
Joined: Wed Aug 03, 2005 10:28 am
Location: Arkansas, USA

Post by toasty2 »

Oh, I didn't realize I had to search for the whole line, I wasn't relating that to a partial search. That thought didn't cross my mind.

Everah's function works and does the job. Thanks everyone.
Post Reply