2015-08-14 53 views
0

我有以下字符串:PHP - stristr找到完全匹配的字符串

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

我的問題是文件包含follownig:

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

是'如果(stristr($線, '/' + $項))'適用於這種情況?當然,最好是預先做一次。 – raina77ow

+0

嘗試使用preg_match作爲'/(?<!\ - )(P90 \ -ASH \ -WOOD \ -WELL \ -WORN)\ b /' –

+0

否$'line ='http://csgo.steamanalyst.com/id/2018/SOUVENIR-P90-ASH-WOOD-WELL-WORN''和'$ item ='P90-ASH-WOOD-WELL-WORN'' – user3662307

回答

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

0

添加這個小的代碼

  • 爆炸以 '/' 行成陣列。

  • 使用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; 
         } 
        } 
    } 
    
+0

'print_r($ lineArr); Array([0] => http:[1] => [2] => csgo。steamanalyst.com [3] => id [4] => 2018 [5] => SOUVENIR-P90-ASH-WOOD-WELL-WORN) – user3662307

+0

你想表達什麼? –

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 

似乎工作...

+0

這仍然使用'http:// csgo.steamanalyst.com/id/2018/SOUVENIR-P90-ASH-WOOD-WELL-WORN'而不是'http://csgo.steamanalyst.com/id/919/P90- ASH-WOOD-WELL-WORN' – user3662307

+0

奇怪!!!怎麼樣? –

0

我的回答是使用end來只抓取explode的字符串的最後部分,然後用^來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