String Function

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

Zanne
Forum Newbie
Posts: 15
Joined: Mon Feb 04, 2008 3:38 pm

String Function

Post 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?
Last edited by Zanne on Wed Jun 11, 2008 12:50 pm, edited 1 time in total.
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: String Function

Post by hansford »

use strstr() http://www.php.net
Zanne
Forum Newbie
Posts: 15
Joined: Mon Feb 04, 2008 3:38 pm

Re: String Function

Post by Zanne »

Tried that and it returns false.
Zanne
Forum Newbie
Posts: 15
Joined: Mon Feb 04, 2008 3:38 pm

Re: String Function

Post 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);
?>
 
Last edited by Weirdan on Fri Jun 13, 2008 1:47 pm, edited 1 time in total.
Reason: php tags
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: String Function

Post 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
 
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: String Function

Post by superdezign »

strpos() also works, and returns the position of the needle in the haystack as well.
User avatar
lonelywolf
Forum Commoner
Posts: 28
Joined: Tue Jun 10, 2008 6:15 am

Re: String Function

Post by lonelywolf »

i think you can use preg_match() to extract filename array from file log1, after that, using strstr() to check..
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: String Function

Post 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. ;)
User avatar
lonelywolf
Forum Commoner
Posts: 28
Joined: Tue Jun 10, 2008 6:15 am

Re: String Function

Post 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 :| !
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: String Function

Post by superdezign »

But preg_match() can already do what strstr() does, plus some. Using both is overkill.
Zanne
Forum Newbie
Posts: 15
Joined: Mon Feb 04, 2008 3:38 pm

Re: String Function

Post by Zanne »

Can you show me how to use preg_match with my program above?
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: String Function

Post 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
}
Zanne
Forum Newbie
Posts: 15
Joined: Mon Feb 04, 2008 3:38 pm

Re: String Function

Post 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.
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: String Function

Post 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
 
Last edited by Weirdan on Fri Jun 13, 2008 1:50 pm, edited 1 time in total.
Reason: php tags
Zanne
Forum Newbie
Posts: 15
Joined: Mon Feb 04, 2008 3:38 pm

Re: String Function

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