私は正規表現にまったく慣れていませんが、正規表現がこれを解決する方法だと思います:
イタリアの住所を含む PHP 文字列で分割する必要があります。
それらは次のように構成されています。
通りの名前、番号 郵便番号 都市
次のように、印刷用に2行に分割する必要があります。
通りの名前、番号
郵便番号市区町村
可能ですか?
preg_match('/^([^,]+, [^ ]+) (.*)/', $text, $matches);
echo $matches[1] . "\n" . $matches[2];
これを試してみてください:
preg_match('/^(.+,.+) (.+ .+)$/', $text, $matches);
「Street Name, Number」を に$matches[1]
、「ZipCode City」を に配置し$matches[2]
ます。
で試してくださいexplode()
。例:
$str = 'Street Name, Number ZipCode City';
$ar_str = explode(', ', $str);
$ar2_str = explode(' ', $ar_str[1], 2);
$ar_str[0] .= ', '. $ar2_str[0];
// First needed substring is in $ar_str[0], seccond substring in $ar2_str[1]
// test
echo $ar_str[0] .'<br/>'. $ar2_str[1];