我正面临一种情况,如果地址长于80个字符,我需要将address
分为两部分,即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";
希望有人可以帮助我!在此先感谢:)
答案 0 :(得分:2)
一个好的开始可能是:
// 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);
}
答案 1 :(得分:2)
动态正则表达式是最直接/最适合此任务的工具。
代码:(Demo)
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
的{{1}}上设置了硬性限制,只是为了明确/安全。
我以前在展示放置好的省略号时曾推荐这种技术(了解更多):How to add an ellipsis hyperlink after the first space beyond 170 characters?
答案 2 :(得分:1)
如果地址总是被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));
}
答案 3 :(得分:0)
这是我的主意 首先,您将所有逗号部分放入一个数组中,然后对其进行迭代,以查看它们加到前一个长度上是否超过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;