我有以下字符串: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。
是'如果(stristr($線, '/' + $項))'適用於這種情況?當然,最好是預先做一次。 – raina77ow
嘗試使用preg_match作爲'/(?<!\ - )(P90 \ -ASH \ -WOOD \ -WELL \ -WORN)\ b /' –
否$'line ='http://csgo.steamanalyst.com/id/2018/SOUVENIR-P90-ASH-WOOD-WELL-WORN''和'$ item ='P90-ASH-WOOD-WELL-WORN'' – user3662307