-2

I have the following code, which takes a location and extracts the city and state/country out of it:

$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));
    }
}

The address outputs as:

"Johannesburg, South" instead of "Johannesburg, South Africa"..

How can I modify the code above to return the full last part of the address like in the example above? I see that implode catches all the pieces with a comma after it but how can I get the last piece of the address to contain the full state or country name? I don't know where to go from here.

Thank you!

cpcdev
  • 1,130
  • 3
  • 18
  • 45

7 Answers7

1

Something like this?

if (strpos($address_input, ',') !== false) {
    $arr = explode(', ', $address_input);
    if (count($arr) > 1) {
        $address = implode(', ', array_slice($arr, -2));
    }
}
Oliver Nybo
  • 560
  • 1
  • 6
  • 24
Sergey Bogdanov
  • 631
  • 1
  • 4
  • 11
  • 3
    An answer is not complete if you don't include an explanation of what your code does, or does differently than OPs code. – Andreas Feb 21 '19 at 07:43
  • It always amazes me when an OP picks the answer with no explanations at all. – Andreas Feb 21 '19 at 07:48
1

Just take 2 and 3 element from exploded array

$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];
}
RainDev
  • 1,098
  • 8
  • 9
1

Provided that the input addresses will always follow the same pattern and that it is only ever the first portion of the address that is to be removed there are several differnt approaches one could take - this being another variation:

    $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;
    }

outputs:

Johannesburg, South Africa
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
1

Try this if the pattern of the is same as mentioned one, i.e Primary Address, City, Country.

$address_input = "555 Test Drive, Johannesburg, South Africa";
print_r(implode(", ", array_slice(explode(", ", $address_input), -2)));

//Output:
//Johannesburg, South Africa 

Working Code is here: http://codepad.org/rgerYEeD

Rohit Ghotkar
  • 803
  • 5
  • 17
0

The problem with your code is the explode you have on the last item. You are basically taking South Africa, split it using space, then take the first word South and replace the last entry of the original array.

$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));
    }
}
RealSollyM
  • 1,530
  • 1
  • 22
  • 35
0

You can use substr_count to see if there is more than one comma in the string.
If there is the substring the string from the first comma +2 characters (the comma and white space) with strpos.
This means we only do string manipulation and no conversions to array and back to string.

$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

Not sure what to do if the string is not in that pattern, you need to include that in the question if you need help with it.

https://3v4l.org/gGWSR

Andreas
  • 23,610
  • 6
  • 30
  • 62
0

You need to take only 2 elements from the address after explode, you can remove the first one and use implode the rest array elements

$address = "555 Test Drive, Johannesburg, South Africa";
$addressToArray = explode(',',$address);
if(is_array($addressToArray)){
  array_shift($addressToArray);
  echo implode(',',$addressToArray);
}

Result

Johannesburg, South Africa
Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
  • An answer is not complete if you don't include an explanation of what your code does, or does differently than OPs code. – Andreas Feb 21 '19 at 07:46