PHP拆分街道和门牌号



所以我需要拆分街道名和门牌号,所以如果这是字符串:

"样本街道12">

我想把街道和号码分开:

"样本街道"12">

但如果门牌号码中有一个字母,比如1A,它需要显示为:

"样本街道"1A">

我尝试使用:

$straat = $order->get_shipping_address_1();
$straat = preg_replace("/[^A-Z]+/", "", $straat);

对于街道,

和:

$str = $order->get_shipping_address_1();
preg_match_all('!d+!', $str, $matches);

对于数字,但它只返回1个字符,或者如果门牌号码中有一个字母,它会跳过它。

您可以使用一个匹配任何内容的模式,直到数字后面跟着任意数量的字符(dw*(。它还使用单词边界来划分不同的数字部分。。。

$straat = 'Sample street 1A';
preg_match_all('!(.*)b(dw*)b!', $straat, $matches);
print_r($matches);

给出

Array
(
[0] => Array
(
[0] => Sample street 1A
)
[1] => Array
(
[0] => Sample street 
)
[2] => Array
(
[0] => 1A
)
)

最新更新