Convert MySQL's POINT to text in PHP - php

Using PHP, how can I convert a value in POINT datatype to a string like POINT (-34.601020 -58.371020) (an ouput in WKT or GeoJSON is preferable)
If I echo the raw value, I get weird characters.
I've tried using bin2hex and then tried to convert the hex to string but with no luck.
I'm aware of MySQL's AsText(), but I would like to do it in PHP.

Finally I've got this working!!!
I had to use unpack in order to extract the binary data from MySQL
$point_value = $data_from_db["point_field"];
$coordinates = unpack('x/x/x/x/corder/Ltype/dlat/dlon', $point_value);
echo $coordinates['lat'];
echo $coordinates['lon'];
Here is a tutotial that helped me with this issue

Related

PHP json_encode scientific format

I am trying to encode the number 17342846 but every time i get 1.7342846e+07 instead of the int format. i am using
$var = 17342846;
json_encode((int)$var);
in php trying to get it to work but still no luck. Any help would be appreciated.
just encode it as string format then convert it later.
json_encode($var + '');

PHP - How to serialize data with special characters from database?

i have a string with special characters, like:
$text = "NÃO";
When i use serialize($text), returns
a:1:{i:0;s:4:"NÃO";}
but when i use a string that i get from my database, like:
$query = SELECT special_text FROM ...
(...)
$text = $row->"special_text"
serialize($text);
returns
a:1:{i:0;s:3:"NÃO";}
, what is crashing my script.
what i have to do when i serialize data from database?
Thx, and sorry for my english
If you don't want to use utf-8, try to encode serialized string before saving it to db, and decode after retrieve it. You may use base64 encoding, just if you use any 8-bit encoding in your db. This method increases data size by 4/3. Or you may use binary (BLOB) field in your db and encode your data with gzip or something like that. Very useful for large amount of text, but increases CPU load.

Converting Hebrew characters to UTF-8 using PHP

i have a MySQL field value with a json object containing Hebrew characters like this:
[{"name":"אספנות ואומנות","value":1,"target":null},{"name":"אופניים","value":2,"target":null}]
(the one in the name field)
This field output is giving me some trouble with a certain web interface.
so, looking around in the database i found another field containing json object and its output works fine.
[{"name":"\u05d0\u05e1\u05e4\u05e0\u05d5\u05ea \u05d5\u05d0\u05d5\u05de\u05e0\u05d5\u05ea","value":1,"target":null},{"name":"\u05d0\u05d5\u05e4\u05e0\u05d9\u05d9\u05dd","value":2,"target":null}]
So i would like to convert the first field to this encoding to see if its solves the output issue.
what is this encoding ? is it UTF-8 ? how can i convert it using PHP ?
i tried to isolate the value and convert it to UTF-8 using
echo iconv("Windows-1255","UTF-8",'אספנות ואומנות');
but its just returning an empty value.
Any help would be great
So, in PHP
json_encode('אספנות ואומנות');
did the trick

Accessing POST data

I'm very new to PHP but have a good understanding of C,
When I want to access some post data on an API i'm creating in PHP I use:
$_POST['date_set']
to fetch a value being passed for date - This all works perfectly, however I read I should be fetching it like this:
$date_set = trim(urldecode($_POST['date_set']));
This always returns a 00:00:00 value for the date after it's stored in my DB.
When I access directly using $_POST['date_set'] I get whatever value was posted, for example: 2013-08-28 10:31:03
Can someone tell me what I'm messing up?
You should try it like,
$date_set = $_POST['date_set'].explode(' ');//('2013-08-28 10:31:03').explode(' ')
echo $date_set[1];
or
echo date('H:i:s',strtotime($_POST['date_set'])));
//echo date('H:i:s',strtotime('2013-08-28 10:31:03'));
If you are very new in php the Read date()
You only run urldecode over data is URL encoded. PHP will have decoded it before populating $_POST, so you certainly shouldn't be using that. (You might have to if you are dealing with double-encoded data, but the right solution there should be to not double encode the data).
trim removes leading and trailing white-space. It is useful if you have a free form input in which rogue spaces might be typed. You will need to do further sanity checking afterwards.
urldecode — Decodes URL-encoded string
Description
string urldecode ( string $str )
Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
urldecode: is used only for GET requests. you should be fine using $_POST['date_set'] only.
http://php.net/manual/en/function.urldecode.php
You'd better do this way
if(isset($_POST['date_set'])){
$date_set = $_POST['date_set'];
}
then you can use $date_set how you want.
If you still get 00:00:00 for $date_set, the problem is coming from the code which provide you the $_POST value.

Is PHP serialize function compatible UTF-8?

I have a site I want to migrate from ISO to UTF-8.
I have a record in database indexed by the following primary key :
s:22:"Informations générales";
The problem is, now (with UTF-8), when I serialize the string, I get :
s:24:"Informations générales";
(notice the size of the string is now the number of bytes, not string length)
So this is not compatible with non-utf8 previous records !
Did I do something wrong ? How could I fix this ?
Thanks
The behaviour is completely correct. Two strings with different encodings will generate different byte streams, thus different serialization strings.
Dump the database in latin1.
In the command line:
sed -e 's/latin1/utf8/g' -i ./DBNAME.sql
Import the file converted to a new database in UTF-8.
Use a php script to update each field.
Make a query, loop through each field and update the serialized string using this:
$str = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $str);
After that, I was able to use unserialize() and everything working with UTF-8.
To unserialize an utf-8 encoded serialized array:
$array = #unserialize($arrayFromDatabase);
if ($array === false) {
$array = #unserialize(utf8_decode($arrayFromDatabase)); //decode first
$array = array_map('utf8_encode', $array ); // encode the array again
}
PHP 4 and 5 do not have built-in Unicode support; I believe PHP 6 is starting to add more Unicode support although I'm not sure how complete that is.
You did nothing wrong. PHP prior to v6 just isn't Unicode aware, and as such doesn't support it, if you don't beat it to be (i.e., via the mbstring extension or other means).
We here wrote our own wrapper around serialize() to remedy this. You could, too, move to other serialization techniques, like JSON (with json_encode() and json_decode() in PHP since 5.2.0).

Categories