我面临的情况是,如果地址超过80个字符,我需要将address
平分为2个部分,即address1
和address2
。
我能够用strlen
添加以下条件来查找地址是否大于80个字符,但是当地址长度超过80个字符时,在逗号之后平均划分地址时会遇到问题。
我的代码如下: -
// Sample Address
$address = "House No. 1234-ABC, Second Floor, XYZ Building, ABC Mall Road, Street Number 1234, ABC Area";
$address_length = strlen($address);
if(address_length > 80){
// Split Long Address Code
$address_equal_length = $address_length/2;
$address1 = substr($address,0,$address_equal_length);
$address2 = substr($address,$address_equal_length,$address_length);
}
不幸的是,上面的代码不会检查逗号并拆分地址如下: -
$address1 = "House No. 1234-ABC, Second Floor, XYZ Buildin";
$address2 = "g, ABC Mall Road, Street Number 1234, ABC Area";
你可以看到它切割Building
字并在Buildin
和$address1
开头的g
结尾显示为$address2
我想将$address
分成如下: -
$address1 = "House No. 1234-ABC, Second Floor, XYZ Building";
$address2 = "ABC Mall Road, Street Number 1234, ABC Area";
希望可以有人帮帮我!提前致谢 :)
一个好的开始可能是:
// Sample Address
$address = "House No. 1234-ABC, Second Floor, XYZ Building, ABC Mall Road, Street Number 1234, ABC Area";
$address_length = strlen($address);
if($address_length > 80){
// Split Long Address Code
$address_equal_length = $address_length/2;
$commaIndex = strrpos(substr($address,0,$address_equal_length),',');
$address1 = substr($address, 0,$commaIndex);
$address2 = substr($address,$commaIndex + 1);
}
您需要测试$ commaIndex是否存在以确保substr不会失败。
编辑
优化的解决方案,在字符串的中心查找最接近的逗号:
<?php
// Sample Address
$address = "House No. 1234-ABC, Second Floor, XYZ Building, ABC Mall Road, Street Number 1234, ABC Area";
$address_length = strlen($address);
if($address_length > 80){
// We gonna look for the comma which is the more close to the center of the string
$address_equal_length = $address_length/2;
$comma_index_before = strrpos(substr($address, 0, $address_equal_length), ',');
$comma_index_after = strpos($address, ',', $address_equal_length);
if ($comma_index_before === false) {
$comma_index_before = 0;
}
if ($comma_index_after === false) {
$comma_index_after = $address_length;
}
$comma_index = $address_equal_length - $comma_index_before <= $comma_index_after - $address_equal_length ? $comma_index_before : $comma_index_after;
$address1 = substr($address, 0, $comma_index);
$address2 = substr($address, $comma_index + 1);
}
动态正则表达式是此任务最直接/最合适的工具。
代码:(Demo)
$address = "House No. 1234-ABC, Second Floor, XYZ Building, ABC Mall Road, Street Number 1234, ABC Area";
$length = strlen($address); // 91
if ($length > 80) {
$middle = floor(strlen($address) / 2); // 45
[$address1, $address2] = preg_split("~.{{$middle}}[^,]*\K, ?~", $address, 2);
}
在我的演示中,输出来自:
var_export($address1);
echo "\n";
var_export($address2);
是:
'House No. 1234-ABC, Second Floor, XYZ Building'
'ABC Mall Road, Street Number 1234, ABC Area'
我使用var_export()
来表示这些值,以证明此解决方案包括在拆分点进行空白修剪。
模式细分是:
.{{$middle}}
评估为.{45}
意味着匹配任何字符的45次出现
[^,]*
匹配零个或多个非逗号字符
\K
重新启动全字符串匹配,也就是忘记以前匹配的字符
, ?
匹配逗号分割,并可选择跟随它的空格
我正在写preg_split()
的2
的硬限制只是明确/安全。
我之前推荐过这种类型的技术,当提出一个很好的省略号时(更多信息):How to add an ellipsis hyperlink after the first space beyond 170 characters?
如果地址总是除以2个逗号,则可以使用explode和array_chunk来获取许多项目。然后使用implode重新组装零件。
这也适用于2个以上的地址。
$address = "House No. 1234-ABC, Second Floor, XYZ Building, ABC Mall Road, Street Number 1234, ABC Area";
$address_length = strlen($address);
if ($address_length > 80) {
$parts = explode(',', $address);
list($address1, $address2) = array_map(function($x) {
return trim(implode(',', $x));
}, array_chunk($parts, 3));
}
这将是我的想法首先你把所有的逗号部分放在一个数组中,然后迭代它们,看看它们是否添加到前一个长度超过80,如果你转到下一行
$addressParts=explode(",",$address);
$endString="";
$currentLine="";
foreach($addressParts as $addressPart){
if(str_len($currentLine.$addressPart)>80){
$endString=$endString.","$currentLine."<br>";
$currentLine=$addressPart;
}else{
if($currentLine != ""){
$currentLine=$currentLine.", ".$addressPart;
}else{
$currentLine=$currentLine.$addressPart;
}
}
}
$endString=$endString.$currentLine;
echo $endString;