I want to break line before all 5 chars numbers. How can I do that ? what I got:
$line[1] = preg_replace('[0-9]{5}', '
\1',$line[1]);
but it seems to don't work.Does anyone know how can I do that ? here is my string example:
Zum Sonnenhügel 19 37688 Beverungen
so from this I want to do this:
Zum Sonnenhügel 19
37688 Beverungen
I have google doc spreadsheet with a column of addresses like this. but I can not to change it there, so I downloaded it as csv and i want to edit it in php
the error I'm getting:
[Symfony\Component\Debug\Exception\ContextErrorException]
Warning: preg_replace(): Unknown modifier '{' in ...\Command\EditCsvCommand.php line 54
Need to speicfy regular expression delimiter, capturing group to use backreference.
$line[1] = preg_replace('/([0-9]{5})/', "
\1", $line[1]);
In addition to that Use double quote to make to be interpreted correctly.
I used /
, but you can choose other characters.
UPDATE
As Blackhole commented, if you use $0
(the text matched), you don't need to use capturing group:
$line[1] = preg_replace('/[0-9]{5}/', "
$0", $line[1]);
You need to use $1
instead of \1
:
$line[1] = preg_replace('/([0-9]{5})/', "
$1", $line[1]);
Also can use a lookahead to insert " "
at the desired position:
$line[1] = preg_replace('/(?=\b\d{5}\b)/', "
", $line[1]);
Added \b
word boundaries to separate the \d
part better.