I have a string as follows
$str = 'asdasdasd,sdfsdfsdf myNumber=1234, 2323 dfdfdf9898 sdfsdfdsf 234';
I'd like to return the digits within myNumber=1234
.
Desired Outcome
$str = '1234';
I currently use the following regex preg_replace('/\myNumber=\d+/', '', $y)
to replace when required, which works perfectly, but I'm not sure how I could use this to extract the numbers after the =
sign from myNumber=1234
?
You can use a preg_match
with the following regex:
\bmyNumber=(\d+)
See the regex demo
The value will be available in Group 1. (\d+)
matches and captures 1+ digits into a group that will be part of the resulting array.
Demo:
$re = '~\bmyNumber=(\d+)~';
$str = "asdasdasd,sdfsdfsdf myNumber=1234, 2323 dfdfdf9898 sdfsdfdsf 234";
preg_match($re, $str, $m);
echo $m[1];