Find something in a file and get the line number?
Moderator: General Moderators
Find something in a file and get the line number?
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.
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?
Exactly how?GWsux wrote:I'm searching an array produced by file().
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:
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.
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
Last edited by toasty2 on Wed Apr 11, 2007 7:25 am, edited 1 time in total.
- aaronhall
- DevNet Resident
- Posts: 1040
- Joined: Tue Aug 13, 2002 5:10 pm
- Location: Back in Phoenix, missing the microbrews
- Contact:
array_search() should work
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:
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?
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
Last edited by toasty2 on Wed Apr 11, 2007 5:25 pm, edited 1 time in total.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.
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.txtfeyd 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.
Its around line 20 and I'm sure it matches completely with what I am searching.
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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>';
?>- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Just to be clear on why array_since() isn't working as you expected, the array $cpuz_report had a row with the value ofGWsux wrote: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.txtfeyd 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.
Its around line 20 and I'm sure it matches completely with what I am searching.
Code: Select all
Specification AMD Athlon(tm) XP 2000+