-2

Lets say, this is my address 537 Great North Road Grey Lynn Auckland City Auckland.

I want to put comma (,) after Grey Lynn and Auckland City

Then address will 537 Great North Road, Grey Lynn, Auckland City, Auckland

How can I do it in PHP? When the length is not fixed.

EM-Creations
  • 4,195
  • 4
  • 40
  • 56
  • 2
    Are you looking for a formula to work out where to put commas in an address, or are you looking for a way to insert commas at some known positions in a string? – Martin Cook Nov 09 '17 at 08:27
  • 3
    Where is the string coming from and what have you tried doing yourself? – Sean Konig Nov 09 '17 at 08:27
  • PHP provides a handful ways to do this. Have you tried something? – axiac Nov 09 '17 at 09:55
  • Some hints: [`str_replace()`](http://php.net/manual/en/function.str-replace.php), [`preg_replace()`](http://php.net/manual/en/function.preg-replace.php), [`explode()`](http://php.net/manual/en/function.explode.php)+[array functions](http://php.net/manual/en/ref.array.php)+[string concatenation](http://php.net/manual/en/language.operators.string.php)+[`implode()`](http://php.net/manual/en/function.implode.php) etc. – axiac Nov 09 '17 at 09:58
  • If all you have is the address in a string like this "537 Great North Road Grey Lynn Auckland City Auckland" without any more information, I'm afraid there is no way to achieve what you're trying to do programmatically. – Frédéric Clausset Nov 09 '17 at 11:02

4 Answers4

0

This is not a perfect solution but you can get an idea how you deal with it.

By using PHP :

$t = "537 Great North Road Grey Lynn Auckland City Auckland";
$t = str_replace(
      ["Road", "Lynn", "City"], // neddle
      ["Road,", "Lynn,", "City,"], // replace
      $t
);
echo $t;

More Details

Rashedul Islam Sagor
  • 1,943
  • 14
  • 20
0

I would suggest you look at Regular Expressions (RegEx) to achieve this.

In that way you could loop through each address and use the regex pattern to replace where a comma is required.

However, I believe due to the format of the data it might be very hard to actually achieve this. The only thing you have to detect where a comma needs to go is a space, and that isn't reliable as you can have spaces between road names etc where you don't want commas to be placed!

If you can I would suggest splitting the data up, so rather than having the address in one string you have it split in separate columns / variables, for "house number", "street", "town" etc.. That way you could then use a simple string concatenation to place the commas where they should go.

E.g.:

$houseNumber . " " . $street . ", " . $town . ",";

I hope that helps!

EM-Creations
  • 4,195
  • 4
  • 40
  • 56
0

Try This Before and after variable you can put comma.

<?php 
$GreyLynn = "Grey Lynn";
$AucklandCity = "Auckland City";
echo ' , '.$GreyLynn.' , '.$AucklandCity;
?>
imtaher
  • 430
  • 4
  • 9
0
$seperate = "537 Great North Road Grey Lynn Auckland City Auckland";

$replace = str_replace ("Grey Lynn", ",Grey Lynn, ",$seperate);

$location =  str_replace `("Auckland City", "Auckland City, ",$replace);`

Result:

537 Great North Road ,Grey Lynn, Auckland City, Auckland