Page 1 of 1
Find something in a file and get the line number?
Posted: Tue Apr 10, 2007 10:45 pm
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.
Re: Find something in a file and get the line number?
Posted: Tue Apr 10, 2007 11:00 pm
by volka
GWsux wrote:I'm searching an array produced by file().
Exactly how?
Posted: Wed Apr 11, 2007 6:54 am
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.
Posted: Wed Apr 11, 2007 7:21 am
by aaronhall
Posted: Wed Apr 11, 2007 4:22 pm
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?
Posted: Wed Apr 11, 2007 4:29 pm
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.
Posted: Wed Apr 11, 2007 4:34 pm
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.
Posted: Wed Apr 11, 2007 5:31 pm
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.
Posted: Wed Apr 11, 2007 6:09 pm
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>';
?>
Posted: Wed Apr 11, 2007 6:43 pm
by toasty2
Thanks, it works. And I turned it into an easy to use function

Posted: Wed Apr 11, 2007 6:53 pm
by RobertGonzalez
Cool.
Posted: Wed Apr 11, 2007 8:07 pm
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.
Posted: Wed Apr 11, 2007 8:14 pm
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.