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.
Help Random Numbers in a .txt or .rtf
Moderator: General Moderators
Re: Help Random Numbers in a .txt or .rtf
Have you tried running it?WetCode wrote:This is the code i`am using dont know if its all corect.
Re: Help Random Numbers in a .txt or .rtf
yes it runs and i get no errors so i think the code is corect.tasairis wrote:Have you tried running it?WetCode wrote:This is the code i`am using dont know if its all 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
Well, yeah.WetCode wrote:But i whene i use echo = "$k"; i only get 1 of the lines containing numbers.
The last one in the file
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
yes i did this is how it looks now.tasairis wrote:Well, yeah.WetCode wrote:But i whene i use echo = "$k"; i only get 1 of the lines containing numbers.
The last one in the file
Since that variable doesn't have what you want, have you tried looking at another variable? Like the one you print_r()d?
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";
?>