Web Info and IT Lessons Blog...

Monday 1 December 2014

PHP String Conversion Functions

The previous lesson of PHP Lessons Series was about PHP String Comparison Functions and in this lesson we will learn about PHP String Conversion Functions.

PHP String Conversion Functions

strtolower()

strtolower() function is used to convert each upper case letter in a string to lower case. The basic structure strtolower() function is given below:



strtolower(string);


Pass a string in the above strtolower() function for functionality. An example usage of strtolower() function is given below:



$str = strtolower("THIS IS A TEST STRING");


The above strtolower() function will convert each letter of the string "THIS IS A TEST STRING" to lower case i.e "this is a test string". The output string returned will be saved in the string variable $str.

strtoupper()

strtoupper() function is used to convert each lower case letter in a string to upper case. The basic structure strtoupper() function is given below:



strtoupper(string);


Pass a string in the above strtoupper() function for functionality. An example usage of strtoupper() function is given below:



$str = strtoupper("this is a test string");


The above strtoupper() function will convert each letter of the string "this is a test string" to upper case i.e "THIS IS A TEST STRING". The output string returned will be saved in the string variable $str.

ucfirst()

ucfirst() function is used to convert the first character of a string to upper case. The basic structure ucfirst() function is given below:



ucfirst(string);


Pass a string in the above ucfirst() function for functionality. An example usage of ucfirst() function is given below:



$str = ucfirst("this is a test string");


The above ucfirst() function will convert the first character of the string "this is a test string" to upper case i.e "This is a test string". The output string returned will be saved in the string variable $str.

lcfirst()

lcfirst() function is used to convert the first character of a string to lower case. The basic structure lcfirst() function is given below:



lcfirst(string);


Pass a string in the above lcfirst() function for functionality. An example usage of lcfirst() function is given below:



$str = lcfirst("This is a test string");


The above lcfirst() function will convert the first character of the string "This is a test string" to lower case i.e "this is a test string". The output string returned will be saved in the string variable $str.

ucwords()

ucwords() function is used to convert the first character of each word in a string to upper case. The basic structure ucwords() function is given below:



ucwords(string);


Pass a string in the above ucwords() function for functionality. An example usage of ucwords() function is given below:



$str = ucwords("this is a test string");


The above ucwords() function will convert the first character each word of the string "this is a test string" to upper case i.e "This Is A Test String". The output string returned will be saved in the string variable $str.

join()

join() function is used to join the array elements into a string. The basic structure join() function is given below:



join("separator","Array Element 1","Array Element 2","Array Element 3");


Pass the separator between the array elements and the arrays elements to join in the above join() function for functionality. An example usage of join() function is given below:



$arr = array('This','is','a','test','string');
$str = join(" ",$arr);


The above join() function will convert the array ($arr) into a string ($str) with space as separator between the array elements. The output of the above join() function will be "This is a test string".

str_split()

str_split() function is used to split each character of string into an array element. The basic structure str_split() function is given below:



str_split(string);


Pass the string you want convert to array in the above str_split() function for functionality. An example usage of str_split() function is given below:



$str = 'This is a test string';
$arr = str_split($str);


The above str_split() function will convert the string ($str) into array ($arr) with each character of the string as an element of the array. The output of the above str_split() function will be:

Array ( [0] => T [1] => h [2] => i [3] => s [4] => [5] => i [6] => s [7] => [8] => a [9] => [10] => t [11] => e [12] => s [13] => t [14] => [15] => s [16] => t [17] => r [18] => i [19] => n [20] => g )

Related Posts
PHP String Comparison Functions
PHP String Functions
More about PHP String Functions
How to remove backslashes in php?
How to add backslashes in php?
Explode and Implode functions in PHP

No comments:

Post a Comment