PHP 内爆地址丢失地址的最后一部分
PHP Implode address losing last part of address
我有以下代码,它获取一个位置并从中提取城市和 state/country:
$address_input = "555 Test Drive, Johannesburg, South Africa";
if (strpos($address_input, ',') !== false) {
$arr = explode(", ", $address_input);
if(count($arr) >1){
$arr[count($arr)-1] = explode(" ", $arr[count($arr)-1])[0];
$address = implode(", ", array_slice($arr, -2));
}
}
地址输出为:
"Johannesburg, South" 而不是 "Johannesburg, South Africa"..
如何将上面的代码修改为 return 地址的完整最后一部分,如上例所示?我看到内爆在它后面用逗号捕获了所有片段,但我怎样才能让地址的最后一段包含完整的州或国家名称?我不知道从这里去哪里。
谢谢!
是这样的吗?
if (strpos($address_input, ',') !== false) {
$arr = explode(', ', $address_input);
if (count($arr) > 1) {
$address = implode(', ', array_slice($arr, -2));
}
}
只需从分解数组中取出 2 和 3 元素
$address_input = "555 Test Drive, Johannesburg, South Africa";
if (strpos($address_input, ',') !== false) {
$arr = explode(", ", $address_input);
if (count($arr) == 3)
$address = $arr[1].', '.$arr[2];
}
假设输入地址始终遵循相同的模式,并且只是要删除地址的第一部分,则可以采用多种不同的方法 - 这是另一种变体:
$address_input = "555 Test Drive, Johannesburg, South Africa";
$arr=explode( ",", $address_input );
if( !empty( $arr ) ){
array_shift( $arr );
$address_output=implode( ",", $arr );
echo $address_output;
}
输出:
Johannesburg, South Africa
您的代码的问题是 explode
您在最后一项上的问题。您基本上是使用 South Africa
,使用 space 拆分它,然后使用第一个单词 South
并替换原始数组的最后一个条目。
$address_input = "555 Test Drive, Johannesburg, South Africa";
if (strpos($address_input, ',') !== false) {
$arr = explode(", ", $address_input);
if(count($arr) >1){
//$arr[count($arr)-1] = explode(" ", $arr[count($arr)-1])[0]; // by commenting this line out, I got he results you were expecting
$address = implode(", ", array_slice($arr, -2));
}
}
如果模式与上述相同,请尝试此操作,i.e
主要地址、城市、国家/地区。
$address_input = "555 Test Drive, Johannesburg, South Africa";
print_r(implode(", ", array_slice(explode(", ", $address_input), -2)));
//Output:
//Johannesburg, South Africa
工作代码在这里:http://codepad.org/rgerYEeD
可以用substr_count看字符串中是否有多个逗号
如果有子字符串,则字符串从第一个逗号+2个字符(逗号和白色space)开始,带有strpos.
这意味着我们只进行字符串操作,不进行数组和字符串的转换。
$address_input = "555 Test Drive, Johannesburg, South Africa";
if(substr_count($address_input, ",") >1){
$address = substr($address_input, strpos($address_input, ",")+2);
}
echo $address;
// Johannesburg, South Africa
不确定如果字符串不在该模式中该怎么办,如果您需要帮助,则需要将其包含在问题中。
爆炸后你只需要从地址中取出2个元素,你可以去掉第一个并使用内爆其余数组元素
$address = "555 Test Drive, Johannesburg, South Africa";
$addressToArray = explode(',',$address);
if(is_array($addressToArray)){
array_shift($addressToArray);
echo implode(',',$addressToArray);
}
结果
Johannesburg, South Africa
我有以下代码,它获取一个位置并从中提取城市和 state/country:
$address_input = "555 Test Drive, Johannesburg, South Africa";
if (strpos($address_input, ',') !== false) {
$arr = explode(", ", $address_input);
if(count($arr) >1){
$arr[count($arr)-1] = explode(" ", $arr[count($arr)-1])[0];
$address = implode(", ", array_slice($arr, -2));
}
}
地址输出为:
"Johannesburg, South" 而不是 "Johannesburg, South Africa"..
如何将上面的代码修改为 return 地址的完整最后一部分,如上例所示?我看到内爆在它后面用逗号捕获了所有片段,但我怎样才能让地址的最后一段包含完整的州或国家名称?我不知道从这里去哪里。
谢谢!
是这样的吗?
if (strpos($address_input, ',') !== false) {
$arr = explode(', ', $address_input);
if (count($arr) > 1) {
$address = implode(', ', array_slice($arr, -2));
}
}
只需从分解数组中取出 2 和 3 元素
$address_input = "555 Test Drive, Johannesburg, South Africa";
if (strpos($address_input, ',') !== false) {
$arr = explode(", ", $address_input);
if (count($arr) == 3)
$address = $arr[1].', '.$arr[2];
}
假设输入地址始终遵循相同的模式,并且只是要删除地址的第一部分,则可以采用多种不同的方法 - 这是另一种变体:
$address_input = "555 Test Drive, Johannesburg, South Africa";
$arr=explode( ",", $address_input );
if( !empty( $arr ) ){
array_shift( $arr );
$address_output=implode( ",", $arr );
echo $address_output;
}
输出:
Johannesburg, South Africa
您的代码的问题是 explode
您在最后一项上的问题。您基本上是使用 South Africa
,使用 space 拆分它,然后使用第一个单词 South
并替换原始数组的最后一个条目。
$address_input = "555 Test Drive, Johannesburg, South Africa";
if (strpos($address_input, ',') !== false) {
$arr = explode(", ", $address_input);
if(count($arr) >1){
//$arr[count($arr)-1] = explode(" ", $arr[count($arr)-1])[0]; // by commenting this line out, I got he results you were expecting
$address = implode(", ", array_slice($arr, -2));
}
}
如果模式与上述相同,请尝试此操作,i.e
主要地址、城市、国家/地区。
$address_input = "555 Test Drive, Johannesburg, South Africa";
print_r(implode(", ", array_slice(explode(", ", $address_input), -2)));
//Output:
//Johannesburg, South Africa
工作代码在这里:http://codepad.org/rgerYEeD
可以用substr_count看字符串中是否有多个逗号
如果有子字符串,则字符串从第一个逗号+2个字符(逗号和白色space)开始,带有strpos.
这意味着我们只进行字符串操作,不进行数组和字符串的转换。
$address_input = "555 Test Drive, Johannesburg, South Africa";
if(substr_count($address_input, ",") >1){
$address = substr($address_input, strpos($address_input, ",")+2);
}
echo $address;
// Johannesburg, South Africa
不确定如果字符串不在该模式中该怎么办,如果您需要帮助,则需要将其包含在问题中。
爆炸后你只需要从地址中取出2个元素,你可以去掉第一个并使用内爆其余数组元素
$address = "555 Test Drive, Johannesburg, South Africa";
$addressToArray = explode(',',$address);
if(is_array($addressToArray)){
array_shift($addressToArray);
echo implode(',',$addressToArray);
}
结果
Johannesburg, South Africa