Welcome Guest, Not a member yet? Register   Sign In
Format into array format. Please help!
#1

[eluser]murphy2006[/eluser]
Hello,

I got a huge list of swedish cities that I need to format the correct way.
I need it to be in an array as below:

Code:
'Fredriksberg','Färnäs','Gagnef','Garpenberg','Horndal'

The current format is:

Code:
Fredriksberg  

  Färnäs  

  Gagnef  

  Garpenberg  

  Horndal

Is there a smart and easy way to format it in the desired way?


Kind Regards,
Daniel
#2

[eluser]Derek Allard[/eluser]
depending on how consistent your data is, the easiest way to do it is to use explode(). You may first need to run a search and replace (str_replace()) on things like tabs and new lines in order to standardize it into "str str str str" instead of "str" with a new line and then "str str" and then a tab... etc.
#3

[eluser]MadZad[/eluser]
Daniel,
Just in case that current format is a file that you're reading, some simple PHP should do the trick - see "file" functions on php.net. Just be sure to trim() each line as it is read in, then skip any empty lines. If there's any characters left, simply add them to your array. That should handle the whitespace issue.

Also, if you use str_replace() to eliminate whitespace, watch out for legitimate spaces, as you wouldn't want to turn "New York" into "NewYork" - I've certainly screwed that up before. (apologies for the ameri-centric example, couldn't see a legit example on http://en.wikipedia.org/wiki/List_of_cities_in_Sweden, so perhaps you won't have that issue)

Gene
#4

[eluser]murphy2006[/eluser]
Derek: Do you know if there is a short example on how a code that takes care of new lines and white space looks like. I am not sure how to write that.

MadZad: There are some small towns that consists of two words but it is not a big problem. Do you know of a code that fixes the problem?

I got all cities in a normal text file where the city name got a tab space infront of it and then an empty line between the city names (one city per row).

Thanks,
Daniel
#5

[eluser]MadZad[/eluser]
Try this:

Code:
$my_file = fopen("C:/towns.txt", "r");
if (!$my_file) {
  echo "ah, bugger";
  exit();
}
$town_arr = array();
while ($my_line = fgets($my_file)) {
  $town = trim($my_line);
  if (empty($town)) continue;
  // $town should contain just the town name, with internal whitespace intact
  $town_arr[] = $town;
}
fclose($my_file);
// Do your processing on $town_arr here

Many different ways to get this done, and can be done more concisely, but this certainly has a shot at working.
#6

[eluser]murphy2006[/eluser]
Hello MadZad!

Thanks for the code. Very kind of you.
I will test it now. Unfortunately I do not know how to do the processing code either.
Is it possible that you can give me a hint on that too?

Thanks,
Daniel
#7

[eluser]MadZad[/eluser]
I've added $town_arr to my previous post - that will leave you with the desired array. By "processing" I mean whatever you want to do with the array, once you have it.

Good luck.
#8

[eluser]murphy2006[/eluser]
Thank you MadZad!

This is how I made it:

Code:
<?php

$my_file = fopen("D:/internet/server/wamp/www/stader.txt", "r");
if (!$my_file) {
  echo "ah, bugger";
  exit();
}
$town_arr = array();
while ($my_line = fgets($my_file)) {
  $town = trim($my_line);
  if (empty($town)) continue;
  // $town should contain just the town name, with internal whitespace intact
  $town_arr[] = $town;

echo '"'.$town.'",';
}
fclose($my_file);
// Do your processing on $town_arr here

?>

Have a fantastic weekend!

Kind Regards,
Daniel




Theme © iAndrew 2016 - Forum software by © MyBB