Home » Php » regex – php preg_match assign result direkt in var

regex – php preg_match assign result direkt in var

Posted by: admin February 25, 2020 Leave a comment

Questions:

iam looking for a way to assign the first result of a preg_match lookup in a variable.

Something like that:

$foo = 'MwSt. DE 19%';
$var = preg_match("/[0-9]*/",$foo)[0]

I would not like to handle an new variable with a new array to get my result of 19.

I know, i can do it so:

$bla = 'MwSt. DE 19%';
preg_match("/[0-9]+/",$bla,$zahl);
var_dump($zahl[0]);

But you see my problem here, i need a new variable $zahl. Is there no smarter way to get the result, one like my first idea ( $var = preg_match(“/[0-9]*/”,$foo)[0] )?

How to&Answers: