PHP String Functions Complete Reference
The predefined math functions in PHP are used to handle mathematical operations with integer and float types. These math functions are built into PHP and do not require any additional installation.
Installation: These functions do not require any special installation. The complete list of PHP math functions is given below:
Example: A program illustrating the decbin() function in PHP:
<?php
echo decbin(45);
?>Output:
101101List of Common PHP String Functions
addcslashes()
Description: Inserts backslashes before specified characters in a string.
Example:
echo addcslashes("Hello, World!", 'o');
// Output: Hell\o, W\orld!addslashes()
Description: Escapes special characters by adding backslashes.
Example:
echo addslashes("Hello 'World'");
// Output: Hello \'World\'bin2hex()
Description: Converts a string to hexadecimal representation.
Example:
echo bin2hex("Hi");
// Output: 4869chop()
Description: Removes whitespace or specified characters from the end of a string.
Example:
echo chop("Hello World! ");
// Output: Hello World!chr()
Description: Converts ASCII value to a character.
Example:
echo chr(65);
// Output: Achunk_split()
Description: Splits a string into smaller chunks.
Example:
echo chunk_split("abcdef", 2, "-");
// Output: ab-cd-ef-convert_uudecode()
Description: Decodes a uuencoded string.
Example:
$encoded = convert_uuencode("Hello");
echo convert_uudecode($encoded);
// Output: Helloconvert_uuencode()
Description: Encodes a string using the uuencode algorithm.
Example:
echo convert_uuencode("Hello");
// Output: 0=&5S=`ITcount_chars()
Description: Returns information about character frequencies in a string.
Example:
print_r(count_chars("Hello", 1));
// Output: Array ( [72] => 1 [101] => 1 [108] => 2 [111] => 1 )crc32()
Description: Calculates the CRC32 polynomial of a string.
Example:
echo crc32("Hello");
// Output: 907060870crypt()
Description: One-way string hashing using algorithms like DES, Blowfish, or MD5.
Example:
echo crypt("mypassword", "salt");
// Output: saGng5vH/kMmQpassword_hash()
Description: Creates a password hash using the bcrypt algorithm.
Example:
echo password_hash("mypassword", PASSWORD_DEFAULT);
// Output: $2y$10$...echo()
Description: Outputs one or more strings.
Example:
echo "Hello", " World!";
// Output: Hello World!explode()
Description: Splits a string by a specified delimiter into an array.
Example:
print_r(explode(" ", "Hello World"));
// Output: Array ( [0] => Hello [1] => World )hex2bin()
Description: Converts a hexadecimal string to binary.
Example:
echo hex2bin("48656c6c6f");
// Output: Helloimplode()
Description: Joins array elements into a single string.
Example:
echo implode(", ", array("apple", "banana", "cherry"));
// Output: apple, banana, cherrylcfirst()
Description: Converts the first character of a string to lowercase.
Example:
echo lcfirst("HELLO");
// Output: hELLOlevenshtein()
Description: Returns the Levenshtein distance between two strings.
Example:
echo levenshtein("kitten", "sitting");
// Output: 3ltrim()
Description: Strips whitespace from the beginning of a string.
Example:
echo ltrim(" Hello");
// Output: Hellomd5_file()
Description: Generates the MD5 hash of a file.
Example:
echo md5_file("example.txt");
// Output: 5d41402abc4b2a76b9719d911017c592md5()
Description: Generates the MD5 hash of a string.
Example:
echo md5("Hello World");
// Output: b10a8db164e0754105b7a99be72e3fe5metaphone()
Description: Computes the metaphone key of a string for phonetic comparisons.
Example:
echo metaphone("example");
// Output: EXMPLnl2br()
Description: Inserts HTML line breaks before newlines in a string.
Example:
echo nl2br("Hello\nWorld!");
// Output: Hello<br />World!