I’m listing out some dates in my PHP application which results in something like the following:
April2016May2016June2016
etc.
I’m trying to use preg_split
to format them like this:
array('April 2016', 'May 2016', 'June 2016')
I used an online Regular Expression editor to determine how to detect for 4 consecutive numbers and here’s how far I’ve gotten:
Note: I am also removing all white space – ideally this would be better if it only removed white space if there were more than 2 spaces i.e. hello world
would not be altered but hello world
would.
preg_split('/dddd/g', preg_replace('!s+!', '', $sidebar_contents));
Using the above, I get an error suggesting that the g identifier is not valid assuming because it’s not preg_match_all
– removing the g result in the following:
Thanks for any help!
Here is a way to achieve what you want with 1 call to
preg_match_all
and using anarray_map
after:See the regex demo and an IDEONE demo.
The pattern means:
(p{L}+)
– match and capture into Group 1 (will be accessible after matching via$m[1]
) one or more letters(d+)
– match and capture into Group 2 (will be accessible after matching via$m[2]
) one or more digits.With
array_map
, we just join the values from Group 1 and 2 with a space.Alternative: fill out the resulting array inside a
preg_replace_callback
(just one pass!):See the IDEONE demo.
You can insert the space and then split:
Outputs:
Try this:
Output: