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="[email protected]";
$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";
  }
}
9
  • 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? Commented Aug 10, 2016 at 6:02
  • 1
    1. strcmp is case sensitive. 2. use === comparison operator Commented Aug 10, 2016 at 6:02
  • 1
    Simple Way shell_exec("grep -h '[email protected]' filename") if you get output then exist else no match found Commented Aug 10, 2016 at 6: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. Commented Aug 10, 2016 at 6:04
  • 2
    @Saurabh shell_exec isn't by default simpler ... especially if you aren't allowed to call it (some hosters are very picky) Commented Aug 10, 2016 at 6:04

3 Answers 3

3

Try this

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
Try to reproduce the problem before you write an answer. This problem has nothing to do with case sensivity
2

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

[email protected]
[email protected]
[email protected]
[email protected]

According to above assumption code should be:-

<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
$toSearch="[email protected]";
$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.

3 Comments

Yeah! it worked, but i want to ask what <pre/> do because i'm just newbie to web programming.
@MoizHusnain check this link for your question:-stackoverflow.com/questions/4756842/…
@Daniel the problem was with the \n
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="[email protected]";
$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";
    }
}
?>

2 Comments

@Anant It's my first question actually, i tired to mark both answers but when i marked the second one your answer was automatically unmarked, it was not my intention.
@MoizHusnain it's ok. I understand. Sorry if my wordings hurted you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.