Page 2 of 3

Re: String Function

Posted: Thu Jun 12, 2008 6:47 pm
by hansford
it won't find anymore because you have a break; statement in the loop which terminates the loop.

Re: String Function

Posted: Thu Jun 12, 2008 7:13 pm
by hansford
I'm going to assume that files2.txt is the materfile that contains all of the possible strings you're looking for in a file.
that being the case then files2.txt should be in the first while loop and logs2 in the second. You have other error code stuff that I commented out. run this code and you get your expected results. We'll work on the error code next after you tell us what the error is for

Code: Select all

 
<?
$text=fopen("logs2.txt", r);
 
while (!feof($text)) {
$tText=fgets($text);
$logs=fopen("files2.txt", r);
while (!feof($logs)) {
$tLogs=fgets($logs);
$error=1;
if(strstr($tLogs, $tText) && $error != 0) {
$error=0;
echo("Found $tText<br>");
//fclose($logs);
break;
} else {
//$error=1;
}
}
if($error == 1) {
//fclose($logs);
}
}
fclose($text);
?>
 

Re: String Function

Posted: Thu Jun 12, 2008 8:36 pm
by hansford
forget my posts - sorry, I see what the problem is doh! here is the corrected code-without the $error var. I made comments where the changes occurred. Oh, and I tested this one :)

Code: Select all

 
<?
$text=fopen("files2.txt", r);
 
while (!feof($text)) {
 
$tText=fgets($text);
$logs=fopen("logs2.txt", r);
while (!feof($logs)) {
 
$tLogs=fgets($logs);
$error=1;
if(strstr($tText,$tLogs)) {  //i haystack before needle
 
echo("Found $tText<br>");
break;
 
}
}
fclose($logs); //close $logs at the end of each loop in the first while
}
fclose($text);
?>
 

Re: String Function

Posted: Thu Jun 12, 2008 9:02 pm
by hansford
of course if all the files in files2.txt are going to have the same format like a.txt which is a single lower-case character of a-z followed by a dot(.) followed by a lower-case string txt
then here is the way to go. You can make a pattern for just about anything and do it this way.

Code: Select all

 
<?
$file1 = file_get_contents("logs2.txt");
$pattern = "#[/s/s]*[a-d]\.txt#";
preg_match_all($pattern, $file1, $matches,PREG_PATTERN_ORDER);
 
foreach($matches[0] as $key){
 
echo "found: " . $key . "<br>";
}
?>
 
results

Code: Select all

 
found: b.txt
found: c.txt
 

Re: String Function

Posted: Fri Jun 13, 2008 7:04 am
by Zanne
I tested what you had, and it still isn't working. I've tried it on my localhost server, and my server http://the-exodus.net. I can't use preg_match, because I'm trying to find files that aren't being used on my web server(other than the-exodus.net). My site currently has over 10,000 files on it, so to narrow down the list, I'm comparing my web logs(logs2.txt) to the directory structure of every single file on the server(files2.txt). Comparing these two files won't give me a 100% list of what to take out, but it should narrow down the list by almost 75%-90%.

Updated the file.php on my server.

File:
http://www.the-exodus.net/help/file.php

File source:
http://www.the-exodus.net/help/viewfile.php

logs2.txt
http://www.the-exodus.net/help/logs2.txt

files2.txt
http://www.the-exodus.net/help/files2.txt


Also, if I use if(strstr($tLogs, tText)), it returns just finding /index.php

Re: String Function

Posted: Fri Jun 13, 2008 9:30 am
by hansford
yeah if(strstr($tLogs,$tText)) won't work b/c you're putting the needle before the haystack. it has to be if(strstr($tText,$tLogs))

I ran the last code I posted against the 2 files you presented when you started this post. And it was 100% accurate. Now if your files don't look like a.txt b.txt ...etc then it won't work.

Re: String Function

Posted: Fri Jun 13, 2008 9:44 am
by Zanne
hansford wrote:yeah if(strstr($tLogs,$tText)) won't work b/c you're putting the needle before the haystack. it has to be if(strstr($tText,$tLogs))

I ran the last code I posted against the 2 files you presented when you started this post. And it was 100% accurate. Now if your files don't look like a.txt b.txt ...etc then it won't work.
Ah, the files2.txt and logs2.txt files are different. I just did a.txt/b.txt as an example, but what's in files2.txt and logs2.txt in the links above is what I need this for.

Re: String Function

Posted: Fri Jun 13, 2008 11:39 am
by RobertGonzalez
If there is another post of code without using code tags I am locking this thread. The code tag is there for a reason. It is hard to read your code when it is in plain text.

It is not that hard. Highlight the code, click the little "Code" button above. Done. To get extra fancy, you can even add a little =php inside the opening code tag to highlight it as PHP.

Re: String Function

Posted: Fri Jun 13, 2008 7:55 pm
by hansford
I put out a post on another forum regarding this, but so far they are just as baffled. I'll let you know if anyone finds an answer to this.
Here is the files and the code I have now.

http://www.modernterrain.com/files2.txt
http://www.modernterrain.com/logs2.txt

Code: Select all

<?
$logs=fopen("logs2.txt", r);
 
while (!feof($logs)) {
 
$tLogs=(string)fgets($logs);
 
$text=fopen("files2.txt", r);
 
while (!feof($text)) {
 
$tText=(string)fgets($text);
 
if(strpos($tLogs,$tText) !== FALSE){
 
echo("Found {$tText}<br>");
break;
}
}
fclose($text);
}
fclose($logs);
?>

Re: String Function

Posted: Fri Jun 13, 2008 11:28 pm
by RobertGonzalez
What are you trying to do again?

Re: String Function

Posted: Fri Jun 13, 2008 11:35 pm
by Zanne
Everah wrote:What are you trying to do again?
Overall, I'm trying to create a file that compares two files and returns what files weren't found. I have one file that has a list of every single file(files2.txt) and I have web logs(logs2.txt). Basically I want to create a file that searches every line of files2.txt and compare it to logs2.txt. If a file in files2.txt isn't found in logs2.txt, log it and echo it on a screen.

Re: String Function

Posted: Fri Jun 13, 2008 11:44 pm
by RobertGonzalez
file() will read a file into an array line by line. You could use that to compare values of the lines of the file line by line.

If the files are going to contain the exact same line information (I mean the lines would look identical in the way the line is written), you could potentially run a shell command that uses the `diff` command and capture the output.

Re: String Function

Posted: Sat Jun 14, 2008 1:32 am
by hansford
Respectfully, when you test the code against the files we have, you will see what is going on. This is not some trivial complaint from people who started programming last year. I'm sure you can come to some conclusion as to what is wrong, whether it be a bug or other.

Re: String Function

Posted: Sat Jun 14, 2008 2:32 am
by Benjamin

Code: Select all

 
<?php
$skeleton_array = explode("\n", file_get_contents('skeleton_file.txt'));
 
$test_array = explode("\n", file_get_contents('test.txt'));
 
while ($line = array_shift($test_array))
{
    if (!in_array($line, $skeleton_array))
    {
        echo "$line is not in skeleton_file.txt\n<br />";
    }
}
 

Re: String Function

Posted: Mon Jun 16, 2008 6:43 am
by hansford
Well DevNet Master beat me to it, but I was going to post this. BTW I still would like to know why it doesn't work the other way.

Code: Select all

 
<?php
 
$haystack = file_get_contents("logs2.txt");
$hayarray = explode("\n", $haystack);
$needle = file_get_contents("files2.txt");
$needarray = explode("\n", $needle);
 
foreach($hayarray as $arg){
 
foreach($needarray as $val){
 
if(strpos($arg, $val) !== false) {
 
    echo "found: " . $val . "<br>";
}
}
}
?>