String Comparison in php is not working

String Comparison in php is not working

Try this

if (strcasecmp ($toSearch,$textData[$i])==0){ //case insensitive comparison
      echo Yes;
}

Doc: http://php.net/manual/en/function.strcasecmp.php

My Assumption:-(your text file looks like this)

moizhusnain@hotmail.com
dfgfdgmoizhusnain111@hotmail.com
moidgdfdffdgzhusnain@hotmail.com
moizdsfdsfdsdfhusnain@hotmail.com

According to above assumption code should be:-

<?php
error_reporting(E_ALL);
ini_set(display_errors,1);
$toSearch=moizhusnain@hotmail.com;
$textData = file(text.txt,FILE_IGNORE_NEW_LINES); // use file() function with ignoring new lines of your text file
foreach($textData as $textDat){ // use foreach() rather than for() loop
  if ($toSearch == $textDat){
    echo Yes;
  }
}
?>

Reference:-

http://php.net/manual/en/function.file.php

Note:- If this works for you that simply means that new lines of your text-file are restricting your code to work as well as strcmp() is not needed actually.

String Comparison in php is not working

While jonju has made a working example that fixes the problem using another approach, it can be fixed with the existing code, merely by using this RegEx (stolen from here)

$string = trim(preg_replace(/ss+/,  , $string));

The following code works:

<?php
$toSearch=moizhusnain@hotmail.com;
$textData=array();
$fla=FALSE;
$file=fopen(text.txt,r) or die(Unable to open file.);
while(!feof($file))
{
  $textData[]=trim(preg_replace(/ss+/,  , fgets($file)));;
}
fclose($file);
for($i=0;$i<count($textData);$i++)
{

    if (strcmp($toSearch,$textData[$i])==0)
    {
        echo Yes;
    }
}
?>

Leave a Reply

Your email address will not be published. Required fields are marked *