【问题标题】:PHP string comparison. RegexPHP 字符串比较。正则表达式
【发布时间】:2014-07-29 09:57:11
【问题描述】:

我们正在尝试显示文件是否包含特定字符串:

我们在这里读取文件:

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

filename.txt 包含“离线”

这里我们尝试比较字符串:

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

我们尝试在没有 strcomp 的情况下使用常规 if,但它也不起作用。我认为 IF 无法将 fread 的结果与常规字符串进行比较。也许我们需要尝试另一种方法。

有什么想法吗?

【问题讨论】:

  • 尝试做一个 $theData 的 var_dump() 以了解类型并获取更多信息。
  • 你试过var_dump($theData)吗?

标签: php regex fopen string-comparison


【解决方案1】:

使用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.";
}

你也可以使用strpos()(在这种情况下更快)

$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'";
}

【讨论】:

    【解决方案2】:

    正则表达式在用于搜索长字符串时非常慢。使用 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";
        }
    

    【讨论】:

      【解决方案3】:

      我提出了另一种方法(取决于文件中的内容),虽然它不是最好的,但在某些情况下它可能有用

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

      再见

      【讨论】:

        【解决方案4】:

        您是否检查过 $theData 中包含的值?

        试试这样的:

        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.";  
        

        这里的文档了解更多信息:http://php.net//manual/en/function.strcmp.php

        或者使用hex494D49的方法:(未测试)

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

        【讨论】:

        • $pattern = "/\b".$initialString."\b/" 或者你会得到Warning: preg_match(): No ending delimiter '/' found in...
        • 我已经用你的建议更新了我的帖子;)谢谢你的建议;)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-01
        • 1970-01-01
        • 2017-04-13
        • 1970-01-01
        • 2019-07-10
        • 1970-01-01
        相关资源
        最近更新 更多