0

Alright, I'm getting data from an external API that scrapes market-data of the Steam market.

Now I'm getting a string as follows,
Desert Eagle | Night (Field-Tested)

Then what I'm trying to do is get what type of weapon it is, what it's name is and what wear it is.

This is what I'm trying to get it to put out,
Desert Eagle
Night
Field-Tested or FT

This is the code I used to do this,

$item_wear = preg_replace('/^.*(\(.*\)).*$/', '$1', $item); //(Field-Tested)
$item_wear_short = preg_replace("/(?![A-Z])./", "", $item_wear); //FT

$exp_item_version = explode(" | ", str_replace($item_wear,"", $item)); //Array of Desert Eagle and Night

$item_version = $exp_item_version[0]; //Desert Eagle
$item_name = $exp_item_version[1]; //Night

That's basically what I've done, but now if there's an item named like,
Chroma Case Key

It'll do all sorts of stuff, while with strings that don't have a wear or anything I'd just like it so the $item_name is Chroma Case Key and the other strings just return empty, any proper way of doing this? also the code I used looks pretty messy in my opinion, anything I could change?

Martijn Ebbens
  • 253
  • 3
  • 15
  • Is the name of the weapon always delimited with '|'? and is the wear always in brackets? – Manuel Otto Aug 08 '17 at 13:08
  • @ManuelOtto that's the problem I'm facing, 95% of the items do contain a wear and do have a name, but some items that are special do not have this and therefor the code will mess up and spit out some weird jibberish. – Martijn Ebbens Aug 08 '17 at 13:09
  • ok, but it would never be something like 'Desert Eagle Night Field-Tested' ? (without | and () ) – Manuel Otto Aug 08 '17 at 13:10
  • No, that certain string would always appear like that, but there are strings like `Chroma Case Key` or `Chrome Case` out there. – Martijn Ebbens Aug 08 '17 at 13:13
  • Ok, wait, lemme try – Manuel Otto Aug 08 '17 at 13:13
  • I would split the string first at `|` and do any matches later, even if the string does not contain any `|` it still returns at least 1 element with the complete string. – xander Aug 08 '17 at 13:15
  • @xander that's actually not that bad, let me give that a shot. – Martijn Ebbens Aug 08 '17 at 13:17
  • @xander true, but it will then return an error on the other variable wouldn't it? the variable that has the exploded item [1] – Martijn Ebbens Aug 08 '17 at 13:20
  • Well I don't have time for a complete answer and it's hard to write it in the comments, but I'd do like `$item_array = explode('|', $item); $item_version = item_array[0];` that always works and then `if (count($item_array) > 1) $item_name = $item_array[1];` then you can further replace the `$item_name` or whatever if there is at least a count of 2 in the array. – xander Aug 08 '17 at 13:24
  • Looks like @ManuelOtto has done that with his answer! :D – xander Aug 08 '17 at 13:25

2 Answers2

2

First you should check wether there is a |, that will tell you wether there is a version & wear in the string, if not, assume the string = item name.

$item_name = '';
$item_version = '';
$item_wear = '';
$item_wear_short = '';


$split = explode('|',$item);

$item_name = $split[0];

if(count($split)==2){ // Has version and possibly wear

    $version_and_wear = $split[1];

    // check for wear
    $wear_index = strpos($version_and_wear, '(');
    if($wear_index!==false){ // contains wear
        $wear_end_index = strpos($version_and_wear, ')',$wear_index);
        if($wear_end_index!==false){
            $item_wear = substr($version_and_wear,$wear_index+1,$wear_end_index-$wear_index-1); // extract wear
            $item_wear_short = preg_replace("/(?![A-Z])./", "", $item_wear); //FT
            $version_and_wear = substr($version_and_wear,0,$wear_index); // remove wear from item string
        }
    }

    $item_version = trim($version_and_wear); //remove whitespaces at the beginning and end of the string
}
Manuel Otto
  • 6,410
  • 1
  • 18
  • 25
1

For the given case you can use pre_split() with three delimiters.

$str="Desert Eagle | Night (Field-Tested)";

$data=preg_split( "/[\(\)|]/", $str );
var_dump($data);

You can extend it further according to your requirements. Here you'll find more explanation if you need.

Haris
  • 764
  • 4
  • 9
  • 27