Jump to content

strpos problem


hansford

Recommended Posts

I have (2) text files. The code is suppose to open logs2.txt and read a line from the file,  it then opens files2.txt and iterates through each line to see if theres a matching substring. If one is found then it just echos out what was found. The code doesn't work as expected. It finds 2 occurenses of matching strings, but I know there's at least 4. I've tried addslashes(), preg_quote() with no luck. I've checked each value of the 2 variables before they enter str_pos()

and they should find a match, but they don't. I know I can use preg_match for this, but I'm curious as to why this is not working.

 

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

 

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

 

I have the follwing code

-------------------------

<?

$logs=fopen("logs2.txt", r);

 

while (!feof($logs)) {

 

$tLogs=fgets($logs);

 

$text=fopen("files2.txt", r);

 

while (!feof($text)) {

 

$tText=fgets($text);

 

if(strpos($tLogs,$tText)){

 

echo("Found {$two}<br>");

break;

}

}

fclose($text);

}

fclose($logs);

?>

Link to comment
Share on other sites

I did it this way, and made it work:

<?php
$content1 = "127.0.0.1 - - [15/May/2008:04:03:23 -0400] \"GET /helpdesk/Admin/index.cfm HTTP/1.1\" 301 330 \"-\" \"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14\"
127.0.0.1 - - [15/May/2008:04:03:23 -0400] \"GET /helpdesk/admin/styles.css HTTP/1.1\" 200 2305 \"-\" \"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14\"
127.0.0.1 - - [15/May/2008:04:03:23 -0400] \"GET /index.cfm?PID=3298 HTTP/1.1\" 200 19268 \"-\" \"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14\"";
$content2 = "/helpdesk/admin/images/arrow_up.gif
/index.cfm
/helpdesk/Admin/index.cfm
/helpdesk/admin/images/minus.gif
/helpdesk/admin/styles.css";
$array1 = explode("\n", $content1);
$array2 = explode("\n", $content2);
foreach ($array2 as $value){
foreach ($array1 as $value2){
	if (strpos($value2, $value)){
		print "found $value\n";
	}
}
}

Link to comment
Share on other sites

be careful with your usage of strpos

 

<?php
$str = 'abcde';

if (strpos($str, 'abc'))
{
echo "found it";
}
else
{
echo "not found";                      // this prints !!!
}
?>

 

You need

 

if (strpos($str, 'abc') !== false)

but, if you do

if(strpos($str, 'abc')){
//this
}
else{
//that
}

it's looking for any value other than false. From PHP.net:

Returns the position as an integer. If needle  is not found, strpos() will return boolean  FALSE.

EDIT

theory isn't the same as practice. ok, it doesn't work the way documented. thx for the clarifcation.

Link to comment
Share on other sites

ok I now have !==false and it can only find 1 out of the possible 4 it should find. If it were a problem with the double quotes, then it wouldn't find anything. If it were a problem with the way chars are compared, it wouldn't find anything. Baffling or maybe just buggy, but I couldn't find any reports for this.

 

<?

$logs=fopen("logs2.txt", r);

 

while (!feof($logs)) {

 

$tLogs=fgets($logs);

 

$text=fopen("files2.txt", r);

 

while (!feof($text)) {

 

$tText=fgets($text);

 

if(strpos($tLogs,$tText) !==false){

 

echo("Found {$two}");

break;

}

}

fclose($text);

}

fclose($logs);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.