php - stristr找到确切的字符串

时间:2015-08-14 09:15:37

标签: php string

我有以下字符串:

P90 | Ash Wood (Well-Worn)

我通过以下内容传达此信息:

$item = str_replace("|","", $item);
$item = str_replace(" (","-",$item);
$item = str_replace(")","",$item);
$item = str_replace(" ","-",$item);
$item = str_replace("--","-",$item);
$item = str_replace("™","",$item);
$item = str_replace("★-","",$item);
$item = str_replace("★","",$item);

返回:

P90-ASH-WOOD-WELL-WORN

我现在将此字符串与字符串文件进行比较以找到匹配项:

$lines = file(public_path().'/csgoanalyst.txt');
foreach ($lines as $line) {
    // Check if the line contains the string we're looking for, and print if it does
    if (stristr($line, $item)) {  // case insensitive
         echo $line;
         break;
    }
}

我遇到的问题是该文件包含以下内容:

http://csgo.steamanalyst.com/id/2018/SOUVENIR-P90-ASH-WOOD-WELL-WORN
http://csgo.steamanalyst.com/id/919/P90-ASH-WOOD-WELL-WORN

这两行包含的匹配都是有效的,但我正在寻找完全匹配 - 在这种情况下是第二个URL。

4 个答案:

答案 0 :(得分:1)

尝试使用preg_match作为

$lines = file(public_path().'/csgoanalyst.txt');
foreach ($lines as $line) {
    // Check if the line contains the string we're looking for, and print if it does
    if(preg_match('/(?<!\-)(P90\-ASH\-WOOD\-WELL\-WORN)\b/',$line)) {  // case insensitive
         echo $line;
         break;
    }
}

Demo

答案 1 :(得分:0)

添加此小代码

  • 用&#39; /&#39;爆炸线进入阵列。

  • 使用end()访问数组的最后一个值。

  • 将此项与项变量进行比较,然后打印(如果匹配)。

      foreach ($lines as $line) {
      // Check if the line contains the string we're looking for, and print    if it does
        if (stristr($line, $item)) {  // case insensitive
          $lineArr = explode('/', $line);
          if(end($lineArr) == $item)
          {
            echo $line;
            break;
          }
        }
     }
    

答案 2 :(得分:0)

我试过了......

 <?php
        $item = "P90-ASH-WOOD-WELL-WORN";
        $lines = array(
            "http://csgo.steamanalyst.com/id/2018/SOUVENIR-P90-ASH-WOOD-WELL-WORN",
            "http://csgo.steamanalyst.com/id/919/P90-ASH-WOOD-WELL-WORN-TEST",
            "http://csgo.steamanalyst.com/id/919/P90-ASH-WOOD-TEST-WELL-WORN",
            "http://csgo.steamanalyst.com/id/919/P90-ASH-WOOD-WELL-WORN"
        );

        foreach ($lines as $line) {

            echo "checking: ".$line."<br/>";

        // Check if the line contains the string we're looking for ...
            if (stristr($line, $item)) {  // case insensitive
                $line_as_array = explode('/', $line);
                if (end($line_as_array) === $item) { // check two string are same
                    echo "matched: ".$line."<br/>";
                    break;
                }
            }
        }
        ?>

并将结果发现为......

checking: http://csgo.steamanalyst.com/id/2018/SOUVENIR-P90-ASH-WOOD-WELL-WORN
checking: http://csgo.steamanalyst.com/id/919/P90-ASH-WOOD-WELL-WORN-TEST
checking: http://csgo.steamanalyst.com/id/919/P90-ASH-WOOD-TEST-WELL-WORN
checking: http://csgo.steamanalyst.com/id/919/P90-ASH-WOOD-WELL-WORN
matched: http://csgo.steamanalyst.com/id/919/P90-ASH-WOOD-WELL-WORN

似乎正在运作......

答案 3 :(得分:0)

我的回答是使用end只抓取explode'd字符串的最后一部分然后用^和$ preg_match它来表示你想要匹配的字符串的开头和结尾。因此,在前端或末端的任何东西都不匹配。只有完全匹配才有效。

<?
$item = "P90-ASH-WOOD-WELL-WORN";
$list = array("http://csgo.steamanalyst.com/id/2018/SOUVENIR-P90-ASH-WOOD-WELL-WORN","http://csgo.steamanalyst.com/id/919/P90-ASH-WOOD-WELL-WORN");

foreach ($list as $line)
{
    if (preg_match("/^$item$/",end(explode("/",$line))))
    {
        echo $line;
        break;
    }
}
?>

结果:

http://csgo.steamanalyst.com/id/919/P90-ASH-WOOD-WELL-WORN