PHP String Formatting Functions
Here now I am about to explain different string functions. This functions will be useful to format the string, convert from one character set to another character set.
Below are the functions:
convert_cyr_string()crc32()hebrev()hebrevc()money_format()number_format()
Lets get into each one:
convert_cyr_string()
This function will convert string from one Cyrillic character set to another
This functions takes first argument as a string, second argument is source Cyrillic character set and third argument is target cyrillic character set.
Here are supported characters set.
- k – koi8-r
- w – windows-1251
- i – iso8859-5
- a – x-cp866
- d – x-cp866
- m – x-mac-cyrillic
Usage :
1 | echo convert_cyr_string('The string', 'k','i'); |
Note: This function is Binary Safe.
crc32()
This function calculates the crc32 polynomial of a string
Usage :
1 2 | $checksum = crc32("This is the string."); printf("%u\n", $checksum); |
hebrev()
This function converts logical hebrev text to visual text.
This will return the visual string.
hebrevc()
This function convert logical Hebrew text to visual text with newline conversion
This function return the visual string.
money_format()
This function will format the number as a currency string.
Basically this function will return the formatted version of the number.
Usage:
1 2 3 4 | $number = 9999.89; setlocale(LC_MONETARY, 'en_US'); echo money_format('%i', $number); // Output : USD 9,999.89 |
number_format()
This function will return the formatted verison of the number.
For this function first parameter will be number to be formatted. Second parameter is used to set the number of decimal points.
Third parameter defines the separator for decimal and last parameter defines the thousand separator.
Note: This function will either take one, two or four arguments but not THREE.
Usage:
1 2 3 4 5 | $number = 9996.89; echo number_format($number); # Output : 9,997 echo number_format($number, 2, ',', ' '); # Output : 9 996,89 |
More From Avinash
Avinash Recommends
- PHP Coding Tips (refulzphp)
- Important String Functions in PHP – String Casing (refulzphp)
