Ayxan Əmiraslanlı
Ayxan Əmiraslanlı

Reputation: 433

php preg match find string inside " " with limit

This is my text:

pagination},queryId:"472f257a40c653c64c666ce877d59d2b", val:"598f257a40c653c64c666ce877d59d2b"

I need to find 472f257a40c653c64c666ce877d59d2b and 598f257a40c653c64c666ce877d59d2b.

These are all between ". I need ONLY strings between " ", and which don't exceds 32.

Here is my code:

preg_match_all('/"^(.*?){32}$"/mis', $get, $results);

Upvotes: 0

Views: 120

Answers (2)

The fourth bird
The fourth bird

Reputation: 163352

You could use :"\K[a-f\d]{32}(?=")

Explanation

  • Match :"
  • Reset the starting point of the reported match \K
  • Match characters from a to f and from 0 to 9 [a-f\d]{32}
  • A positive lookahead that asserts what follows is a " (?=")

For example:

$re = '/:"\K[a-f\d]{32}(?=")/';
$str = 'pagination},queryId:"472f257a40c653c64c666ce877d59d2b", val:"598f257a40c653c64c666ce877d59d2b"';
preg_match_all($re, $str, $matches);
var_dump($matches[0]);

That would result in:

array(2) {
  [0]=>
  string(32) "472f257a40c653c64c666ce877d59d2b"
  [1]=>
  string(32) "598f257a40c653c64c666ce877d59d2b"
}

Output php example

Upvotes: 1

Srdjan M.
Srdjan M.

Reputation: 3405

Regex: "([a-z0-9]{32})" or (?<=")[a-z0-9]{32}(?=")

$text = "pagination},queryId:\"472f257a40c653c64c666ce877d59d2b\", val:\"598f257a40c653c64c666ce877d59d2b\"";

preg_match_all("/\"([a-z0-9]{32})\"/", $text, $match);
print_r($match);

Output:

Array
(
    [0] => Array
        (
            [0] => "472f257a40c653c64c666ce877d59d2b"
            [1] => "598f257a40c653c64c666ce877d59d2b"
        )

    [1] => Array
        (
            [0] => 472f257a40c653c64c666ce877d59d2b
            [1] => 598f257a40c653c64c666ce877d59d2b
        )
)

Upvotes: 0

Related Questions