Web Info and IT Lessons Blog...

Monday, 24 November 2014

PHP String Functions

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

PHP String Functions

PHP has many pre-defined string functions used to perform various operations on strings. Some of the PHP string functions are discussed below:

How to find the length of a PHP String?

If this is the question on your mind then the answer to your question is strlen() function. strlen() function is used to calculate the length of a PHP string. Just pass the string in the strlen() function to find out the length of the string. The use of strlen() function is given below:



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


The variable $str in the above code will contain the length of the string returned by strlen() function.

Output of the above strlen() function is:

22

How to reverse a PHP String?

If this is the question that just crossed your mind then all you need to know about is strrev() function. strrev() function is used to reverse a PHP string. Just pass the string you want to reverse in the strrev() function. The use of strrev() function is given below:



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


The variable $str in the above code will contain the reversed string returned by strrev() function.

Output of the above strrev() function is:

!gnirts tset a si sihT

How to find the number of words in a PHP String?

The answer to the above question is str_word_count() function. str_word_count() function is used to calculate the number of words in a PHP string. Pass the string in the str_word_count() function to find out the number of words. The use of str_word_count() function is given below:



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


The variable $str in the above code will contain the number of words returned by str_word_count() function.

Output of the above str_word_count() function is:

5

How to find the ASCII value of a character in PHP?

The answer to the above question is ord() function. ord() function is used to calculate the ASCII value of a character in PHP. Pass the character in ord() function to find out it's ASCII value. If we pass a string in the ord() function, then the ASCII value of only first character will be returned by the ord() function. The use of ord() function is given below:



$str = ord("T");
$str2 = ord("This is a test string!");


The variables $str and $str2 in the above code will contain the ASCII values returned by ord() function.

Outputs of the above ord() functions are:

84 and 84 as in both cases the ASCII values of 'T' will be calculated.

How to find the character from the ASCII value in PHP?

The answer to the above question is chr() function. chr() function is used to return the character from the ASCII value in PHP. Pass the decimal, octal or the hexadecimal value in chr() function to find out the character. The use of chr() function is given below:



$char = chr(104);
$char2 = chr(067);
$char3 = chr(0x47);


The variables $char,$char2 and $char3 in the above code will contain the characters returned by chr() function.

Outputs of the above chr() functions are:

h,7 and G respectively.

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

No comments:

Post a Comment