I would like to create a regular expression that will replace a normal space with hard-breaking space according to my country typographical rules. It shouldn’t touch HTML tags.
All spaces in these examples should be hard-breaking:
Numbers
1 000
10 000
100 000
1 000 000
etc..
Dates
17. 6.
17. 6. 2012
I came up with:
$pattern = '/((\d\.?)\s(\d))(?=[^>]*(<|$))/';
$text = preg_replace($pattern, '$2 $3', $text);
It can do all of these but:
17. 6. 2012
It won’t replace the second space, so result is ->
17. 6. 2012
Thank you for any help!
The
(\d)is eating the first trailing digit, and if the trailing number only has a single digit, there won’t be enough of it left to match on the next pass.For instance, the following work fine:
12 34 56,12. 34. 56. But these do not:1 2 3,1. 2. 3The
(\d)can be moved into the lookahead, to avoid eating it:Combining this with @flec’s use of a lookbehind yields: