Trim is a lifesaver

Emir Buğra KÖKSALAN tarafından tarihinde yayınlandı

Selection_004I’m working on an application that parsing an XML document. Actually this is an KML document and it has some coordinate informations. I’m reading this document via SimpleXML. This is very usefull and easy library. Thanks to PHP Team for this library but there is a very very small thing that when you get data in a tag that can return the whitespace characters. For example New Line Character “\n”, Tab character “\t”, Space character ” “ etc.

Let me explain this issue with an example. Look at that tag:

.......
<coordinates>
373123.738058839,4194744.78479568,0 373123.62460879,4194740.76741263,0 
</coordinates>
......

What do you think about this? Did you see any wrong thing in here? No. I didn’t see too. Ok when you read this tag with any XML parser library (my favorite library is SimpleXML) it returns that data (You imagine there is no quotes):

"
373123.738058839,4194744.78479568,0 373123.62460879,4194740.76741263,0
"

I added quotes for you but in real you can’t see them. Now is there any error? You can not see any error. You imagine that you can split this data with Comma “,” or Space Character ” “. Yes of course you can. Let split it with Space and then Comma:

// read the data from the SimpleXML object
$coordsRaw = $simpleXmlObj->....->coordinates;

// split with space character
$coordsSpaceSplitted = explode(" ", $coordsRaw);
print_r($coordsSpaceSplitted);

This prints an array that has three items. Last item will be empty string. Why? Becouse there is a space character at last. Ok lets continue again. We will split every item with Comma and cast every data to float.

$coordsSplitted = array();
foreach ($coordsSpaceSplitted as $k => $v) {
  $commaExploded = explode(",", $v);
  $coordsSplitted[] = array(
    "x" => (float)$commaExploded[0],
    "y" => (float)$commaExploded[1]
  );
}

You think there is no any error aren’t you? I will show you the trick. You imagine the first item will be “373123.738058839”. Let me show you:

echo $coordsSplitted[0][x];

This prints “0”. Whaaaaat? Wa wa what the f. going on maaan? Where did I do error? Every loop is correct and assignments are too. Yes all of our code correct. But the mistake is at the top. YOU MUST THINK ABOUT THE WHITE SPACE CHARACTERS. You forget the New Line Character “\n” at the $commaExploded[0]. This variable contains that data: “\n373123.738058839” and we’re trying to cast this data to “float”. If you try to New Line Character to “float” it’s new value will be “0”. You can look here.

How can we solve this proble? There is a very small solution for this issue. I found this solution after waste my lots of hours. The asnwer is when you cast a string to a number value you have to TRIM it. If we trim the data at the top of the code then we would not get any cast error. Actually this is not an error this is just a “trick”. The first line of code must be like this:

$coordsRaw = trim($simpleXmlObj->....->coordinates);

Good luck.

Kategoriler: PHP

Emir Buğra KÖKSALAN

Java & PHP Developer

0 yorum

Bir yanıt yazın

Avatar placeholder

E-posta adresiniz yayınlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir

Time limit is exhausted. Please reload the CAPTCHA.

Bu site, istenmeyenleri azaltmak için Akismet kullanıyor. Yorum verilerinizin nasıl işlendiği hakkında daha fazla bilgi edinin.