user6668201
user6668201

Reputation: 77

PHP text to number or int

hello guys im new to this forum so i a little confused about giving the title ( and im sorry if this already asked somewhere i just dont know where to find it ) , i heard that returning string / text to mysql database is not that safe , so i just want to turn it into number , but i already search troughout google and cannot find what i was looking for , example i have text :

"this is text within a quotes"

i want to turn it into number or something like :

"2421512152142152134"

but what i got from php its returned 0 instead a number it was supposed to be : ( Screenshot )

You can edit this word to anything u like , just don't break stackoverflow rules ;)

i cannot find any method to do this , can anyone tell me what is the method to do it ?

i also want it work to all text like utf-8 , utf-7 , or something like that , not just listed text .

just a note , i hate decimal .

Upvotes: 0

Views: 140

Answers (2)

Oliver
Oliver

Reputation: 893

This will never work due to restrictions of interger fields in Mysql-Database. It it is explained here: https://dev.mysql.com/doc/refman/5.7/en/integer-types.html

In short words. If you like to parse a string like this "2421512152142152134" to an integer value, it fails, because the maximum number is 2147483647 or 4294967295 for unsigened integers.

You can try Bigint, but it is not safe, if you get long int or numbers.

Upvotes: 0

Nicolas Roehm
Nicolas Roehm

Reputation: 704

I don't know anything about why returning string / text to mysql database is not safe. Maybe if you have some SQL in that string, but in this case you need to escape your string to prevent it to be execute. If you really want to transform your string for security reasons, i think some encryption / hash method can help you.

hash() Example

echo hash('ripemd160', 'The quick brown fox jumped over the lazy dog.');

The above example will output:

ec457d0a974c48d5685a7efa03d137dc8bbde7e3

Upvotes: 2

Related Questions