How to create regex (pattern) in PHP to match the exact format of this string:
MORE 8
Where 8 is an id.
As an example, I have the following code:
$id = 8;
$keyword = MORE;
$string = $keyword." ".$id;
Now, I need a regex pattern to match my $string variable.
$regex_pattern = "/MORE ([0-9]*)/";
If you want to retrieve the id (i.e. 8
) from the string MORE 8,
you can use the regexp /MORE ([0-9]*),/
.
Example:
$string = "MORE 8,";
$regexp = "/MORE ([0-9]*),/";
$matches = [];
if(!preg_match($regexp, $string, $matches)) {
die("Input string doesn't matches the regexp");
}
$id = $matches[0]; // $id = 8