I'm trying to extract the text in the following way:
$subname = "subarray({value=subarray({0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48}, EXCEL*48, 1)";
preg_match('#\{(.*?)\}#',$subname, $match, PREG_OFFSET_CAPTURE);
print_r($match[1][1]);
$matchs = substr( $subname, 0, $match[1][1]);
print_r($matchs);
I would like to obtain the following text from $subname
I'm struggling with the 2nd bit (getting the EXCEL word). I wonder whether it is possible to get preg_match to give me the rest of the string?
You may use
'#\{([\s\d.,]*)},\s*(\p{L}+)(\*\d+)#'
See the regex demo.
Details:
\{
- a {
([\s\d.,]*)
- Group 1 capturing 0+ whitespaces, digits, commas and dots}
- a literal }
,
- a comma\s*
- 0+ whitespaces(\p{L}+)
- Group 2: one or more letters(\*\d+)
- Group 3: a *
and 1+ digits.See a PHP demo below.
$subname = "subarray({value=subarray({0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48}, EXCEL*48, 1)";
$res = array();
if (preg_match('#\{([\s\d.,]*)},\s*(\p{L}+)(\*\d+)#',$subname, $match)) {
$res = explode(", ", $match[1]);
array_push($res, $match[2]);
array_push($res, $match[3]);
}
print_r($res);