2

I am trying to open a file and compare each line to a string to see if they are same or not, but it's not working, here is the code.

$toSearch="moizhusnain@hotmail.com";
$textData=array();
$fla=FALSE;
$file=fopen('text.txt','r') or die('Unable to open file.');
while(!feof($file))
{
  $textData[]=fgets($file);
}
fclose($file);
for($i=0;$i<count($textData);$i++)
{
  echo $textData[$i]."<br/>";
  if (strcmp($toSearch,$textData[$i])==0)
  {
      echo "Yes";
  }
}
Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
Moiz Husnain
  • 50
  • 10
  • 3
    I assume, there is a newline (`\n`) at the end of each `$textData[$i]`, so it probably won't match? Why do you use strcmp anyway? – Jakumi Aug 10 '16 at 06:02
  • 1
    1. `strcmp` is case sensitive. 2. use `===` comparison operator – rokas Aug 10 '16 at 06:02
  • 1
    Simple Way `shell_exec("grep -h 'moizhusnain@hotmail.com' filename")` if you get output then exist else no match found – Saurabh Aug 10 '16 at 06:02
  • 1
    Instead of loading your data line for line into an array, do the comparison directly in `while`. What you are doing is overkill and you are wasting memory and cpu. – Charlotte Dunois Aug 10 '16 at 06:04
  • 2
    @Saurabh `shell_exec` isn't by default simpler ... especially if you aren't allowed to call it (some hosters are very picky) – Jakumi Aug 10 '16 at 06:04
  • 1
    @CharlotteDunois you are probably meaning well, but I just assumed this is a minimal example and OP didn't bother to optimize it further. – Jakumi Aug 10 '16 at 06:05
  • 1
    @Jakumi Yes that may be the case. But if it available we could use it to search for string in file. – Saurabh Aug 10 '16 at 06:06
  • 1
    I have actually tried that too, means in the while loop, and i have also tried '===' but it was not showing the desired results. – Moiz Husnain Aug 10 '16 at 06:12
  • 1
    You can use php function : strcasecmp() as well. – Vijaysinh Parmar Aug 10 '16 at 06:29

3 Answers3

3

Try this

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

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

rokas
  • 1,491
  • 9
  • 16
jonju
  • 2,691
  • 1
  • 12
  • 19
  • This presumes that the problem is case-sensitivity. What if it is not? Doesn't a trailing newline seem more obvious (not "visual" to casual inspection, whereas a n uppercase letter tends to stick out when you say to yourself "why aren't these two things the same"). Still, three votes for it and counting. – Bill Woodger Aug 10 '16 at 06:07
  • it's still not working. It actually works when i just compare two stings but unfortunately in this case when i compare a string with an element of array this is not working. – Moiz Husnain Aug 10 '16 at 06:08
  • Try to reproduce the problem before you write an answer. This problem has nothing to do with case sensivity – Daniel Aug 10 '16 at 06:09
2

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.

Anant Kumar Singh
  • 68,309
  • 10
  • 50
  • 94
0

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('/\s\s+/', ' ', $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('/\s\s+/', ' ', fgets($file)));;
}
fclose($file);
for($i=0;$i<count($textData);$i++)
{

    if (strcmp($toSearch,$textData[$i])==0)
    {
        echo "Yes";
    }
}
?>
Community
  • 1
  • 1
Daniel
  • 9,741
  • 12
  • 40
  • 75