Answer the question
In order to leave comments, you need to log in
How to split text into groups of 3 digits with a regular expression?
Actually, the task is to divide any number into groups with a space, for example: 1 234 or 123 456 789, you understand.
The solution, it seems, is obvious: we find three digits at the end and use the looking back, which meets the condition: "if there are digits in front, then put a space." In my mind it was like this:
$var = 123456789;
echo preg_replace('/(?=[0-9])[0-9]{3}$/', '-$0', $var); // пробел заменён на дефис для наглядности
// на выходе: 123456-789
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question