Find rows of numbers in a .txt or .rtf file and output the line numbers of file.

First of let me just say hello to all you members this is my first time here.
Its been some time since i coded PHP so i`am rusty as H##L.
And this is way i hope you fine ppl can help me out.

As the subject says i`am trying to make a script that serches the all of the .txt file fore numbers og any cind.
And then posts the line of all matches found. I have done this to an extent.
White this code. :
[php]<?php
$file = “file.txt”;

$lines = count(file($file));

echo “There are $lines lines in $file

”;
?>

<?php // the file has been uploaded. // (do it yourself) $file = 'file.txt'; // the minimum length of each number $min = 4; $matches = array(); foreach (file ($file) as $k => $line) { if (preg_match ('/^[\d]{' . $min . ',}+$/', $line)) { $matches[] = $k; } } if (sizeof ($matches)) { // dumps line numbers with positive match print_r ($matches); } echo "$matches
$k
$line"; ?>[/php]

But its not doing exacely what i want it outputs the result like this.
There are 8 lines in file.txt

Array ( [0] => 7 ) Array
7
78678
As you can see it only outputs the last line and i want it to post all lines.
As this is a test file ALL the lines in file.txt is suposed to be posted. (All have numbers)
As fore the file i will be using\ serching in i dont need to tink abaout format becuse all i want it to do is check every line fore numbers and output every line that has more then 4 numbers after etche other.
So what i want it to do is post somthing like this:
[b]There are 8 lines in file.txt

On Line: 0 Is: 12222
On Line: 1 Is: 21342
On Line: 2 Is: 12222
On Line: 3 Is: 23222
On Line: 4 Is: 12322
On Line: 5 Is: 12344
On Line: 6 Is: 66346
On Line: 7 Is: 78678[/b]

can anyone please help me white this ? I will be for ever greatfull.

Hi. Try preg_match_all() instead
:wink:

Actually, I was wrong. sorry. I thought you’re trying to find all matches at once. Now I see you are searching in each of lines separately. In this case, probably there is a new line char \n or carriadge return char \r. I’d add \s? so that entire expression look like this:

/^[\d]{' . $min . ',}+\s?$/

p.s. not sure why do you need the + sign here?

Oki so tryd that and got an error :
Warning: preg_match_all() expects at least 3 parameters, 2 given in …/check.php line 19

And i used code like this:
[php]$file = ‘file.txt’;
// the minimum length of each number
$min = 4;

$matches = array();
foreach (file ($file) as $k => $line)
{
if (preg_match_all (’/^[\d]{’ . $min . ‘,}+$/’, $line))
{
$matches[] = $k;
}
}

if (sizeof ($matches))
{
// dumps line numbers with positive match
print_r ($matches);
} [/php]

Oki sorry think i got it to work almost.
It is now outputing:
There are 8 lines in file.txt

Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 ) Array
7
78099

White this code :
[php]<?php
$file = “file.txt”;

$lines = count(file($file));

echo “There are $lines lines in $file

”;
?>

<?php // the file has been uploaded. // (do it yourself) $file = 'file.txt'; // the minimum length of each number $min = 4; $matches = array(); foreach (file ($file) as $k => $line) { if (preg_match ('/^[\d]{' . $min . ',}+\s?$/ ', $line)) { $matches[] = $k; } } if (sizeof ($matches)) { // dumps line numbers with positive match print_r ($matches); } echo "$matches
$k
$line"; ?>[/php]

I codent get Preg_matche_all to work.
Now all i need is it to display the numbers in text\ numbers after etch line number.
And also how whode i go about to making them just show the line number not the hole 5 [6] => 6 [7] => 7
And also place them under ethce other.? And thanks PHP Coder for the quik replay.

Just replace this line:
[php]print_r ($matches);[/php]

with this:
[php]echo implode(’, ',$matches[0]);[/php]

oki i did this and got this error:
Warning: implode() [function.implode]: Invalid arguments passed in …/file.php
Code look like this now:
[php] $matches[] = $k;
}
}

if (sizeof ($matches))
{
// dumps line numbers with positive match
echo implode(’, ',$matches[0]);
}
?>[/php]

This should work:
[php]echo implode(’, ',$matches);[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service