3

We are trying to display whether a file contains a specific string or not:

Here we read the file:

$myFile = "filename.txt";
$fh = fopen($myFile,'r');
$theData = fread($fh, filesize("filename.txt"));
fclose($fh);

filename.txt contains "Offline"

Here we are trying to compare the strings:

if(strcmp($theData,"Online")==0){
echo "Online"; }
elseif(strcmp($theData,"Offline")==0) {
echo "Offline"; }
else {
echo "This IF is not working." }

We have tried using regular if without the strcomp, but it did not work either. I'm thinking that an IF cannot compare the results from the fread to a regular string. Perhaps we will need to try another method.

Any Ideas?

2
  • Try to do a var_dump() of $theData to know the type and get more information.
    – Hoijof
    Jul 29, 2014 at 10:00
  • Did you try var_dump($theData)?
    – hjpotter92
    Jul 29, 2014 at 10:01

4 Answers 4

5

Use preg_match()

$string = "your-string";
$pattern = "/\boffline\b/i"; 

// The \b in the pattern indicates a word boundary, so only the distinct 
// word "offline" is matched; if you want to match even partial word "offline"
// within some word, change the pattern to this /offline/i

if(preg_match($pattern, $string)) {
    echo "A match was found.";
}

You can use strpos() as well (it is faster in this case)

$string = 'your-stringoffline';
$find   = 'offline';
$pos = strpos($string, $find);

if($pos !== false){
    echo "The string '$find' was found in the string '$string' at position $pos";
}else{
    echo "The string '$find' was not found in the string '$string'";
}
0

regex is very slow when used to search in long strings. use strpos

$strFile = file_get_contents("filename.txt"); // load file
if(strpos($strFile, 'Online')!==false){ // check if "Online" exists
    echo "We are Online";
    }
elseif(strpos($strFile, 'Offline')!==false){ // check if "Offline" exists
    echo "We are Offline";
    }
else{ // other cases
    echo "Status is unknown";
    }
0
0

I put another way to do that (depending what it is inside the file), although it is not the best it may be useful in some circumstances

if (exec("grep Offline filename.txt") === 'Offline')
    echo 'Offline';
else
    echo 'Online';

Byee

-1

Are you checked the value contains in $theData ?

Try something like this:

if(strcmp($theData,"Online") === 0)
   echo $theData." is equal to string Online using case sensisive"; 
else if(strcmp($theData,"Offline") === 0)
   echo $theData." is equal to string Offline using case sensisive"; 
else
   echo $theData." This IF is not working.";  

Here the doc for more infos: http://php.net//manual/en/function.strcmp.php

Or using the hex494D49's method: (Not tested)

function isStringAreTheSame($initialString, $stringToCompare) {
   $pattern = "/\b".$initialString."\b/";

   return preg_match($pattern, $stringToCompare);
}
2
  • 1
    $pattern = "/\b".$initialString."\b/" or you'll get Warning: preg_match(): No ending delimiter '/' found in... Feb 9, 2021 at 22:51
  • I have updated my post with your suggestion ;) Thank you for this ;)
    – Lapinou
    Feb 10, 2021 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.