Help Random Numbers in a .txt or .rtf

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
WetCode
Forum Newbie
Posts: 3
Joined: Sun Nov 21, 2010 12:41 pm

Help Random Numbers in a .txt or .rtf

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post by requinix »

WetCode wrote:This is the code i`am using dont know if its all corect.
Have you tried running it?
WetCode
Forum Newbie
Posts: 3
Joined: Sun Nov 21, 2010 12:41 pm

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

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

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

Post 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?
WetCode
Forum Newbie
Posts: 3
Joined: Sun Nov 21, 2010 12:41 pm

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

Post 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
Post Reply