php - Complex regex request - need three variables from string
Get the solution ↓↓↓Solution:
The preg_match function would work well for this assuming the race info is as consistent as you show here. Here is an example code snippet:
$str = '02/07/2019 - RACE 2 SHANE MURPHY PAINTING 366M';
if ( preg_match( '/^([0-9]{2}\/[0-9]{2}\/[0-9]{4}) - RACE ([0-9]{1,}) (.*) ([0-9]{3,}M)$/', $str, $matches ) ) {
echo "Race number: ${matches[2]}\n";
echo "Race date: ${matches[1]}\n";
echo "Race name: ${matches[3]}\n";
echo "Race length ${matches[4]}\n";
} else {
echo "Cannot parse the string\n";
}
This would produce the following output:
Race number: 2
Race date: 02/07/2019
Race name: SHANE MURPHY PAINTING
Race length 366M
Note that it's the matches in parenthesis "()" that ultimately end up in the $matches array passed to the preg_match function. As usual, lots more info available online: https://www.php.net/manual/en/function.preg-match.php
Answer
Solution:
There are two main approaches, the first is to use a regex...
Using([\d\/]{10,}) - RACE (\d*) ([\w ]*) (\d*M)
will match:
([\d\/]{10,})
a combination of 10 numbers and / - which is the date.- RACE (\d*)
which is the literal RACE followed by a numerical race number.([\w ]*)
which is a sequence of letters(and spaces) for the name(\d*M)
a numeric followed by the literal M.
So the code is...
$row = ['race_name' => '28/07/2019 - RACE 2 WYNNUM HAULAGE Maiden F 366M'];
preg_match("/([\d\/]{10,}) - RACE (\d*) ([\w ]*) (\d*M)/",
$row['race_name'],
$race_info);
echo "Race number: {$race_info[2]} <br>";
echo "Race date: {$race_info[1]} <br>";
echo "Race name: {$race_info[3]}<br>";
// need to find last word ending in M (e.g. 600M, 1200M, 40M)
echo "Race length: {$race_info[4]} <br>";
OR, you could just stick to using explode and work out which parts are which. You already have the date and the race number, the last one will be the distance and all of the bit between the race number and the distance is joined back together to give the race name...
$row = ['race_name' => '28/07/2019 - RACE 2 WYNNUM HAULAGE Maiden F 366M'];
$race_info = explode(" ",$row['race_name']);
echo "Race number: {$race_info[3]} <br>";
echo "Race date: {$race_info[0]} <br>";
$length = count($race_info);
// need to find race name (in middle of the string)
$name = implode(" ", array_slice($race_info, 4, $length-5));
echo "Race name: {$name}<br>";
// need to find last word ending in M (e.g. 600M, 1200M, 40M)
echo "Race length: {$race_info[$length - 1]} <br>";
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: 403 this action is unauthorized.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.