String Function
Moderator: General Moderators
String Function
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?
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.
Re: String Function
use strstr() http://www.php.net
Re: String Function
Tried that and it returns false.
Re: String Function
Basically, out of this program, I want to see displayed:
Everything works, besides the comparing part. It's returning nothing found.
Here's what I'm working with:
files2.txt contains:
logs2.txt contains
Here is my PHP file:
Code: Select all
b.txt
c.txt
Here's what I'm working with:
files2.txt contains:
Code: Select all
a.txt
b.txt
c.txt
d.txt
Code: Select all
b.txt
c.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);
?>
Last edited by Weirdan on Fri Jun 13, 2008 1:47 pm, edited 1 time in total.
Reason: php tags
Reason: php tags
Re: String Function
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
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: String Function
strpos() also works, and returns the position of the needle in the haystack as well.
- lonelywolf
- Forum Commoner
- Posts: 28
- Joined: Tue Jun 10, 2008 6:15 am
Re: String Function
i think you can use preg_match() to extract filename array from file log1, after that, using strstr() to check..
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: String Function
After already using preg_match(), strstr() should no longer be necessary.lonelywolf wrote:i think you can use preg_match() to extract filename array from file log1, after that, using strstr() to check..
- lonelywolf
- Forum Commoner
- Posts: 28
- Joined: Tue Jun 10, 2008 6:15 am
Re: String Function
maybe using preg_match 1 time better than 2 times.
, i think if we use strstr for the second time will better. it's my opinion
!
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: String Function
But preg_match() can already do what strstr() does, plus some. Using both is overkill.
Re: String Function
Can you show me how to use preg_match with my program above?
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: String Function
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.
This is equivalent and faster:
Code: Select all
if (preg_match('~' . $needle . '~', $haystack)) {
// Do something
}Code: Select all
if (strstr($haystack, $needle)) {
// Do something
}Re: String Function
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.
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
I ran this code against the 2 files setup like you stated and got this result: Found b.txt
-----------------
logs2.txt
files2.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);
?>
Code: Select all
b.txt
c.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
Reason: php tags
Re: String Function
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.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