Jump to content

Strpos Issue


DaniIvanov

Recommended Posts

Hello

 

I have an issue with strpos. I have the following code

function participantIdFilter ($participantID)
$log = "uploads/log.txt";
$logRead = fopen($log, "r");
$content = file_get_contents($log);
if(strpos($content, $participantID)){
$search = "TTRUE";
} else {
$search = "FFALSE";
}
}

 

The strpos ALWAYS returns false no matter what I do. I know for sure that both $content and $participantID have strings in them. When I change the $participantID within the strpos perimeters with "integers/numbers/ids" it works as expected but as soon as i put in the variable it always result in false. What I am trying to do is open a file and check if participantID is in the file and if it is generate a new one.

Edited by DaniIvanov
Link to comment
Share on other sites

Show us how you're calling the function, and the data you're giving it.

 

Here is how I am calling the function

 

// Check if participantID already exists and return TRUE or FALSE
$some = participantIdFilter(3374);
echo $some;

 

When i know that it works I will change 3374 with a variable that generates a rand number.

I use 3374 right now because i know for certain that it already exists in the LOG file)

 

Here is the FULL function. I cut out a small piece on the end (of the top post) because i did not think that it was relevant for now

 

function participantIdFilter ($participantID) {
$a = $participantID;
$log = "uploads/log.txt";
$logRead = fopen($log, "r");
$content = file_get_contents($log);
if(strpos($content, $a)){
$search = "TTRUE";
} else {
$search = "FFALSE";
}
return $search . "/<br />" . $content;
if($search == 1) {
$participantID = rand(1000, 9999);
}
}

Edited by DaniIvanov
Link to comment
Share on other sites

Note that if your search item is the very first thing in your file, you will get a false result because strpos will return 0 (found at position zero) which is loosely equal to false (which is returned when not found).

 

The proper way to test for whether the item is found or not would be:

if (false !== strpos($content, $a)){
  $search = "TTRUE";
} else {
  $search = "FFALSE";
}

 

Link to comment
Share on other sites

Note that if your search item is the very first thing in your file, you will get a false result because strpos will return 0 (found at position zero) which is loosely equal to false (which is returned when not found).

 

The proper way to test for whether the item is found or not would be:

if (false !== strpos($content, $a)){
$search = "TTRUE";
} else {
$search = "FFALSE";
}

 

I just tried that (also before) and it did not work. It gives FFALSE always even if the value that i pass in to a exists in the log file.

Link to comment
Share on other sites

needle

If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.

 

Since you're passing an int, strpos is searching for the character defined by chr($a), not (string)$a.

 

if (false !== strpos($content, (string)$a)){

 

side note your call to fopen() is completely unnecessary and can be removed.

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.