Reputation: 1170
I have the following string:
P90 | Ash Wood (Well-Worn)
I'm passing this through the following:
$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);
Which returns:
P90-ASH-WOOD-WELL-WORN
I'm now comparing this string against a file of strings to find a 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 (stristr($line, $item)) { // case insensitive
echo $line;
break;
}
}
The problem I have is the file contains the follownig:
http://csgo.steamanalyst.com/id/2018/SOUVENIR-P90-ASH-WOOD-WELL-WORN
http://csgo.steamanalyst.com/id/919/P90-ASH-WOOD-WELL-WORN
both lines contain matches therefor are both valid, however I'm looking for an exact match - in this case the second URL.
Upvotes: 0
Views: 169
Reputation: 21437
Try Using preg_match
as
$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;
}
}
Upvotes: 1
Reputation: 1741
I tried this ...
<?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;
}
}
}
?>
and found the result as ...
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
seems working ...
Upvotes: 0
Reputation: 134
My answer is to use end to only grab the last part of the explode'd string and then preg_match it with ^ and $ to signify beginning and end of the string you want to match. So anything with something on the front or the end will not match. Only exact matches will work.
<?
$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;
}
}
?>
the result:
http://csgo.steamanalyst.com/id/919/P90-ASH-WOOD-WELL-WORN
Upvotes: 0
Reputation: 774
Add this small code
exploding line with '/' into array.
access last value of array with end().
comparing this to item variable then printing if matched.
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;
}
}
}
Upvotes: 0