Page 1 of 3

String Function

Posted: Wed Jun 11, 2008 12:42 pm
by Zanne
Basically, I need a function that can search String1 in String2, and if String1 is found in String2, to return true.

Ex:
$String1="a.txt";
$String2="nadfna.txtafdadf";
Returns True

I want this to search String, not chars!

Any suggestions?

Re: String Function

Posted: Wed Jun 11, 2008 12:44 pm
by hansford
use strstr() http://www.php.net

Re: String Function

Posted: Wed Jun 11, 2008 12:50 pm
by Zanne
Tried that and it returns false.

Re: String Function

Posted: Wed Jun 11, 2008 12:55 pm
by Zanne
Basically, out of this program, I want to see displayed:

Code: Select all

 
b.txt
c.txt
 
Everything works, besides the comparing part. It's returning nothing found.

Here's what I'm working with:
files2.txt contains:

Code: Select all

 
a.txt
b.txt
c.txt
d.txt
 
logs2.txt contains

Code: Select all

 
b.txt
c.txt
 
Here is my PHP file:

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($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: Wed Jun 11, 2008 8:40 pm
by hansford

Code: Select all

 
$String1="a.txt";
$String2="nadfna.txtafdadf";
 
function find_string($haystack, $needle){
 
if(!$ret = strstr($haystack, $needle)){
return 0;
}
return 1;
}
 
$ret = find_string($String2, $String1);
results// $ret == 1
 

Re: String Function

Posted: Wed Jun 11, 2008 8:53 pm
by superdezign
strpos() also works, and returns the position of the needle in the haystack as well.

Re: String Function

Posted: Thu Jun 12, 2008 6:34 am
by lonelywolf
i think you can use preg_match() to extract filename array from file log1, after that, using strstr() to check..

Re: String Function

Posted: Thu Jun 12, 2008 6:42 am
by superdezign
lonelywolf wrote:i think you can use preg_match() to extract filename array from file log1, after that, using strstr() to check..
After already using preg_match(), strstr() should no longer be necessary. ;)

Re: String Function

Posted: Thu Jun 12, 2008 6:48 am
by lonelywolf
maybe using preg_match 1 time better than 2 times. 8O , i think if we use strstr for the second time will better. it's my opinion :| !

Re: String Function

Posted: Thu Jun 12, 2008 7:01 am
by superdezign
But preg_match() can already do what strstr() does, plus some. Using both is overkill.

Re: String Function

Posted: Thu Jun 12, 2008 7:25 am
by Zanne
Can you show me how to use preg_match with my program above?

Re: String Function

Posted: Thu Jun 12, 2008 7:41 am
by superdezign
Using preg_match() at all when the string that you are searching for is known is overkill. Regular expressions aren't exactly the fastest method of searching a string. But, here you go.

Code: Select all

if (preg_match('~' . $needle . '~', $haystack)) {
  // Do something
}
This is equivalent and faster:

Code: Select all

if (strstr($haystack, $needle)) {
  // Do something
}

Re: String Function

Posted: Thu Jun 12, 2008 10:06 am
by Zanne
Sadly, none of those work. Here's what I have so far. All changes I do, you'll be able to see.

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

I've tried preg_match, but because the logs have // and other stuff, I keep getting a syntax error. As you can see, strstr() is returning File Not Found. I may not have it set up right, but please take a look and see what I'm doing wrong.

Re: String Function

Posted: Thu Jun 12, 2008 11:35 am
by hansford
I ran this code against the 2 files setup like you stated and got this result: Found b.txt
-----------------

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($tLogs, $tText) && $error != 0) {
$error=0;
echo("Found $tText<br>");
fclose($logs);
break;
} else {
$error=1;
}
}
if($error == 1) {
fclose($logs);
}
}
fclose($text);
?>
 
logs2.txt

Code: Select all

 
b.txt
c.txt
 
files2.txt

Code: Select all

 
a.txt
b.txt
c.txt
d.txt
 

Re: String Function

Posted: Thu Jun 12, 2008 11:43 am
by Zanne
hansford wrote:I ran this code against the 2 files setup like you stated and got this result: Found b.txt
-----------------
<?
$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($tLogs, $tText) && $error != 0) {
$error=0;
echo("Found $tText<br>");
fclose($logs);
break;
} else {
$error=1;
}
}
if($error == 1) {
fclose($logs);
}
}
fclose($text);
?>
logs2.txt
--------------
b.txt
c.txt
----------------
files2.txt
----------------
a.txt
b.txt
c.txt
d.txt
Thing is, it isn't finding c.txt aswell. It only finds one. Now, if I change the b.txt in logs2.txt to asab.txtatq or something like that, b.txt won't be found, and I need that to be found.