user3280648
user3280648

Reputation: 75

Regex picking to much

This is my json

{
  "market_hash_name" : "Chroma 2 Case",
  "price" : "0.12",
  "created_at" : 1463811431
},
{
  "market_hash_name" : "Chroma 2 Case Key",
  "price" : "2.58",
  "created_at" : 1463820978
},

im executing this regex

        foreach ($prices as $items ){
            foreach ($items as $key => $value){
                if (preg_match("/\A". preg_quote($itemValue['name']) ."/i", $value )) {
                    $x = $items;
                }
            }
        }

However when i only search for Chroma 2 Case it also shows Chroma 2 Case Key

How do i adjust my regex to stop after the string is found?

Upvotes: 1

Views: 24

Answers (2)

Andreas
Andreas

Reputation: 23958

if (preg_match("/\A". preg_quote($itemValue['name']) ."\z/i", $value ))
// or
if (preg_match("/\A". preg_quote($itemValue['name']) ."$/i", $value ))
//or maybe
if (preg_match("/\A". preg_quote($itemValue['name']) ."\"/i", $value ))

One of them should work.
The first one matches to end of line.
The second matches to end of string.
The last one matches if the " is in the string. (Very unsure about this one)

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Change your regex pattern as shown below:

...
if (preg_match("/\A". preg_quote($itemValue['name']) ."$/i", $value )) {
// $ - indicates the end of the string
...

Upvotes: 1

Related Questions