Earon
Earon

Reputation: 823

preg_match_all in php showing result blank

How do i match this with REGEXP and PHP ?

"s:6:\"[\"50\"]\";",
"s:5:\"[\"1\"]\";"

I want to match numbers between : [\"50\"] this only or could be one or more.

I have a pattern and want to take only numbers from json_encode value also serialize() in php this is code :

$result = [];
        foreach($impressions as $impression) {
            preg_match_all('/\x5C/', $impression->subcategories, $result);
        }

return $result;

if no preg_match then here is result :

"s:6:\"[\"50\"]\";",
"s:5:\"[\"1\"]\";"

I am using this to match only digit where \ is so i can take number only like 50 or 1

Any idea how i can pic number with regular expressions ? value hex not works '/\x5C/' showing me result blank but here : Works fine if i put result and test with same REGEXP.

Screenshot

Upvotes: 0

Views: 85

Answers (1)

C3roe
C3roe

Reputation: 96250

First of all, you can not go through an array of strings that way with preg_match_all – your $result array gets overwritten in each loop iteration.

And then, you need to capture the numbers you want to see in your result set. To do that, you must mask the [, ] and \ characters each with another \ – and then capture the digits in the middle by putting them in ( and )

$impressions[] = "s:6:\"[\"50\"]\";";
$impressions[] = "s:5:\"[\"1\"]\";";

foreach($impressions as $impression) {
  preg_match_all('#\[\\"([0-9]+)\\"\]#', $impression, $matches); // I chose # as delimiter
     // here – with so many \ involved, we don’t need / around it to add to the confusion

  $results[] = $matches; // $matches will be overwritten in each iteration, so we
    // preserve its content here by putting it into the $results array
}

var_dump($results);

Upvotes: 2

Related Questions