php array phone number fields stored as int - php

I had a MySQL table with some user data, which I needed to correct and migrate to a new MySQL table. I exported the table using "Export to PHP Array plugin for PHPMyAdmin" from "Geoffray Warnants" and it returned a (PHP) array.
One of the fields contains a telephone number. Now some of the entries have been exported as string. However, some of the entries have the telephone number represented as an integer. When I try to read the number, it returns something like:
4.36991052022E+12
when it should be:
4369910520219
I suppose the integer value is too big, so that must be the problem. (that's the reason for the E+12)
I have close to 300 entries and there is no way I can start writing quotes in front and end of the number manually, since I also have a fax field.
Most recently, I tried (with help of demo sublime text 2) to cast the number by writing (string) in front of it - it doesn't work.
I'm kind of helpless now and ask for your help. What can I do?

Please take a look at this question, which should answer yours:
Convert a big integer to a full string in PHP

Since I didn't have the time to get trough the "complicated" process of installing the GMP library, I decided to make it old-skool and just put double quotes ("") in front of every phone number value no matter if is was a string or a "big integer" and remove (single) quotes from the final string.
Thanks to Sublime Text 2!
So i had:
array(..., 'phone'=>' 43 664 1000383', ...);
and
array(..., 'phone'=>4369910520219, ...);
Search for and Find All 'phone'=> and add afterwards "
Then search for (in my case) ,'fax'=> and add beforehand "
The for every string
preg_replace("/\'/i", "", $user["phone"]);
Thanks though for the library. I might actually use it someday. ;)
Greetings,
Joseph

Related

Regex,Php - find correct names with random numbers in string

Hi i would like to find every string what contains this
SET_X_GAME_X_SCORE
Where X is a random number.
I tried something like this
/^SET_[0-9]_GAME_[0-9]_SCORE(.*)/
but it didn't work.
Any idea? Thanks
Hi i would like to find every string what contains this
If you really mean "contains" then you should stop using the ^ that means begin with
"/SET_[0-9]+_GAME_[0-9]+_SCORE(.*)/"
also a few sample rows within the question would be useful

How can I generate a fix string for a specific name which is associated for that name only

I want to generate fix string for name which is associated for that name only.
For example:
my string: test
generated string: elephant
If I write test again then it must generate string elephant in any browser or in any machine
Could anyone please help me with this?
I don't think jQuery is the right answer, but you could use it for handling XHRs to get the transformed string from a PHP back-end.
It sounds like you want deterministic hashing of strings to dictionary words. Using a single word as your result is far more likely to result in a collision; use 3 or more. Give your PHP back-end access to a large dictionary: a simple, non-profane word list, which must not change, in a file or a database table with a 0-based index. Pass it the input string to hash using something like sha256. Take equal length slices of the hash string and with each slice, look up one word by converting the slice's hexadecimal to an integer, add the integer to the previous index if it's not the first, modulo (%) the integer by the length of the dictionary to get an index and add the word at that index to your result.

Convert numbers to another one in the same range, and back

I'm looking for a solution to convert all numbers in a given range to another number in the same range, and later convert that number back.
More concrete, let's say I have the numbers 1..100.
The easiest way to convert all numbers to another one in the same range is to use: b = 99 -a; later get the original with a = 99 - b;.
My problem is that I want to simulate some randomness.
I want to implement this in PHP, but the coding language doesn't matter.
WHY?
You maybe say why? Good question :)
I am generating some easy to read short code string based on id-s, and because the id's are incremented one by one, my consecutive short codes are too similar.
Later I need to "decode" the short codes, to get the id.
What my algorithm is doing now is:
0000001 -> ababac, 0000002 -> ababad, 0000003 -> ababaf, etc.
later
ababac -> 0000001, ababad -> 0000002, ababaf -> 0000003, etc.
So before I actually generate the short code I want to "randomize" the number as much as possible.
Option 1:
Why dont you just have a database of conversion? i.e each record has a "real" id, and a "random md5" string or something
Option 2:
Use a rainbow table - maybe even a MD5 lookup table for the range 0 - 10,000 or whatever. Then just do a hashtable lookup
Finally I found a solution based on module operator, on the math forum.
The solution can be found here:
https://math.stackexchange.com/questions/259891/function-to-convert-each-number-in-a-m-n-to-another-number-in-the-same-range

php comma and decimal format

I just want to ask if there is any PHP/MySQL datatype that can store a number with a comma and decimal such as 10,000.35
when the user hit the save button with this value, it should be stored in a MySQL table and the system can retrieve it also to be processed as number 10000.35
thanks for any help!
That would be a CHAR/string datatype.
Numeric values don't have formats. They only contain the numeric value. Commas are not relevant for numeric computation. Format the values on output using, for instance, number_format. That's the only time a comma is relevant, it does not need to be stored.
Store the number without the comma in MySQL and just format the number in PHP when you are displaying it. It would be easier to keep the number without the comma in PHP as well if you're doing math with it - only use the comma when displaying the data!
If you are going to be storing numeric values it is best to leave all the formatting out of it.
For instance, what if you need to localize the display so that 10,000.05 needs to be 10.000,05? You'd have a lot of work to do.
You should store the value in the database as 10000.05 and use number_format($myValue,2,'.',','); to display the value when it's time. This will allow you to change the literals to variables or constants should you ever have to localize. It will also allow you to configure how many decimal places you care to display.
Here's the PHP docs for number_format()
It would be best to store it in your MySQL database without the comma, and then using PHP's number_format to display it with the commas.
The MySQL datatype that does this is DECIMAL. DECIMAL gives you fixed decimal places without the precision errors inherent in float type. (ie 123.45 instead of 123.4499999999999999)
You have 2 options , using MySQL to format directly or Using PHP .... see below for examples
MySQL Direct Solution
SELECT FORMAT(number, 2) as formatNumber FROM table ;
PHP Solution
number_format($number,2,'.',',');
Thanks
:)

PHP what is the best approach to split these values up?

Having a hard time with this one as I don't think I know all of my options.
I have to parse a free form text field that I need to map the values to a database.
Here is some example text, NOTE: not all fields have to be there, not all delimiters are the same and not all descriptors are available. I do need to check if the value is numeric only or is it alpha numeric.
Example 1
field1: 999-999234-24-2
field2 Description: a short description
field3: 3.222.1
asdfg
field number four: NO
field5:
Example 2
field1: 999-999234-24-2/field2 Description: a short description/field3: 3.222.1 asdfg/field number four: NO/field5:
Example 3
999-999234-24-2
Example 4
field1: 999-999234-24-2 field2 Description: a short description field3: 3.222.1 asdfg field number four: NO field5:
Example 5
field1: 999-999234-24-2 - field2 Description: a short description - field3: 3.222.1 asdfg - field number four: NO - field5:
What I would like is all fields X to be in there own column. NOTE the example data is all in the same order but live data is not.
Now I don't mind doing this in steps if I need to but having a hard time just parsing the values up into columns. any suggestions?
I was thinking some sort of case function with a RegEx but not luck so far.
Maybe you should standardize on the java .properties format then you can use this PHP example to parse it:
http://www.innerweaver.com/?p=13
Since it's still stuck in my head ... the way I'd go about it is start handling each of these cases and see if there is any remaining tweaks/fallout. What appears to make this tricky is the only reliable deliminator is 'field', and if anyone uses that in a description it'll break. I'd just have to take the file and start iterating.
Splitting it with this regex would at least be a good start point for dividing the headers and the data. Basically, field plus additional optional text that covers the possibility of 'Description' and 'number four' added before the closing :
field[^:]{0,12}:
After that, you'd at least have to strip trailing / for case #2, the ' - ' for case #5, the extra linebreaks if you don't want them in the data for case #1.
RegEXP would be hard to maintain in some edge-cases. Try writing a simple finite state machine
after much though/trial and error I'm going to read them into an array and parse out each line of text. It's long and going to be a mess but should get the job done.

Categories