Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php中的字符串比较不起作用_Php_String - Fatal编程技术网

php中的字符串比较不起作用

php中的字符串比较不起作用,php,string,Php,String,我试图打开一个文件,将每一行与一个字符串进行比较,看看它们是否相同,但它不起作用,下面是代码 $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

我试图打开一个文件,将每一行与一个字符串进行比较,看看它们是否相同,但它不起作用,下面是代码

$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";
  }
}
$toSearch=”moizhusnain@hotmail.com";
$textData=array();
$fla=假;
$file=fopen('text.txt','r')或die('无法打开文件');
而(!feof($file))
{
$textData[]=fgets($file);
}
fclose($文件);
对于($i=0;$i试试这个

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

Doc:

下面给出了我的假设和您必须执行的代码:-

假设:-(您的文本文件如下所示)

所以代码应该是:-

<?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

echo "<pre/>";print_r($textData); // print array
foreach($textData as $textDat){ // use foreach() rather than for() loop
  if ($toSearch == $textDat){
    echo "Yes";
  }
}
?>

参考资料:-


注意:-如果这对您有效,这仅仅意味着文本文件的新行限制了您的代码的工作,并且实际上不需要使用
strcmp()

虽然jonju已经制作了一个使用另一种方法修复问题的工作示例,但是可以使用现有代码来修复,只需使用这个正则表达式()

以下代码起作用:

<?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";
    }
}
?>


我假设,在每个
$textData[$I]
的末尾都有一个换行符(
\n
),所以它可能不匹配?为什么还要使用strcmp?1.
strcmp
区分大小写。2.使用
==
比较运算符简单方式
shell\u exec(“grep-h”moizhusnain@hotmail.com“文件名”)
如果您得到输出,然后存在,否则将找不到匹配项,而不是将数据逐行加载到数组中,直接在
中进行比较,而
。您所做的是过度消耗内存和cpu。@Saurabh
shell\u exec
在默认情况下并不简单……尤其是在不允许您调用它的情况下(一些主持人非常挑剔)这假设问题是区分大小写。如果不是呢?尾随的换行符看起来不是更明显(对随意检查来说不是“可见的”,而当你对自己说“这两个东西为什么不一样”时,n大写字母往往会突出).尽管如此,三票赞成并开始计数。它仍然不起作用。当我只比较两个Sting时,它实际上起作用,但不幸的是,在这种情况下,当我将字符串与数组元素进行比较时,它不起作用。在你写答案之前,尝试重现这个问题。这个问题与区分大小写无关。是的!它起作用了,但我想要问
怎么办,因为我只是网络编程的新手。@MoizHusnain请查看此链接以了解您的问题:-@Daniel问题出在
\n
@Anant这是我的第一个问题实际上,我很想标记两个答案,但当我标记第二个答案时,您的答案会自动取消标记,这不是我的本意。@MoizHusnain没关系。我明白。如果我的话伤害了你,我很抱歉。
$string = trim(preg_replace('/\s\s+/', ' ', $string));
<?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";
    }
}
?>