Page 1 of 1

Help Random Numbers in a .txt or .rtf

Posted: Sun Nov 21, 2010 12:48 pm
by WetCode
Can some one help me cant get this to work.
Trying to make a script that finds all the lines in a .txt or .rtf that contains a line of numbers that has 4 ore more numbers like 1234 on Line: 5 and 12345 on line: 16
This is the code i`am using dont know if its all corect.

<?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);
}
?>

Thanks to all that can help.

Re: Help Random Numbers in a .txt or .rtf

Posted: Sun Nov 21, 2010 2:34 pm
by requinix
WetCode wrote:This is the code i`am using dont know if its all corect.
Have you tried running it?

Re: Help Random Numbers in a .txt or .rtf

Posted: Mon Nov 22, 2010 1:48 am
by WetCode
tasairis wrote:
WetCode wrote:This is the code i`am using dont know if its all corect.
Have you tried running it?
yes it runs and i get no errors so i think the code is corect.
But i whene i use echo = "$k"; i only get 1 of the lines containing numbers.
The last one in the file and it says that the numbers is on line 15 whene its realy on 14.
So its missing the corect line white 1 no huge problem i can live white that.
But i need it to post all the lines numbers that hase a row of numbers 4 ore more.

Re: Help Random Numbers in a .txt or .rtf

Posted: Mon Nov 22, 2010 2:13 am
by requinix
WetCode wrote:But i whene i use echo = "$k"; i only get 1 of the lines containing numbers.
The last one in the file
Well, yeah.

Since that variable doesn't have what you want, have you tried looking at another variable? Like the one you print_r()d?

Re: Help Random Numbers in a .txt or .rtf

Posted: Mon Nov 22, 2010 3:07 am
by WetCode
tasairis wrote:
WetCode wrote:But i whene i use echo = "$k"; i only get 1 of the lines containing numbers.
The last one in the file
Well, yeah.

Since that variable doesn't have what you want, have you tried looking at another variable? Like the one you print_r()d?
yes i did this is how it looks now.

Code: Select all

<?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 "$k";
?>
also tryd echo ="$matches"; but when i do that the site is totoly blank