Web Info and IT Lessons Blog...

Tuesday 25 November 2014

More about PHP String Functions

The previous lesson of PHP Lessons Series was about PHP String Functions and in this lesson we will learn about a little more PHP String Functions.

PHP String Functions

wordwrap()

The wordwrap() fucntion in PHP is used to break a string to a new line when the length of the string exceeds a specific length. Pass the string, the length after which the string breaks to new lines and the break tag in the wordwrap() function for proper functionality. The use of wordwrap() function is given below:



$str = wordwrap('This is a test string!',12,"<br>\n");


The output of the above wordwrap() function is stored in the variable $str.

Output of the above wordwrap() function:

This is a
test string!

nl2br()

nl2br() fucntion is used to break a line whenever there is an occurence of \n in a string. Pass the string with the occurences of \n in the nl2br() function for functionality. The use of nl2br() function is given below:



$str = nl2br("This is a test string.\n And we have a line break.");


The output of the above nl2br() function is stored in the variable $str.

Output of the above nl2br() function:

This is a test string.
And we have a line break.

trim()

trim() function is used in PHP to remove characters and whitespaces from both ends (start and end) of a string. Just pass the string and the characters you want to remove from both ends of the string in the trim() function for functionality. The use of trim() function is given below:



$str = trim("This is a test string!","Tsng!");


The trim() function used above will remove the characters 'T' from the start of the string and 'ng!' from the end of the string. If you want to remove the character 's' of the word 'This', the complete word 'This' needs to be removed as the character in between cannot be removed using trim() function. The output of the above trim() function is stored in the variable $str.

Output of the above trim() function:

his is a test stri

chop()

chop() fucntion is used to remove characters and whitespaces from right end of a string. Pass the string and the characters you want to remove from the right end of the string in the chop() function for functionality. The use of PHP chop() function is given below:



$str = chop("This is a test string!","ng!");


The chop() function used above will remove the characters 'ng!' from the right end of the string. The output of the above chop() function is stored in the variable $str.

Output of the above chop() function:

This is a test stri

ltrim()

ltrim() fucntion is used to remove characters and whitespaces from left side (i.e start) of a string. Pass the string and the characters you want to remove from the start of the string in the ltrim() function for functionality. The use of PHP ltrim() function is given below:



$str = ltrim("This is a test string!","Thi");


The ltrim() function used above will remove the characters 'Thi' from the start of the string. The output of the above ltrim() function is stored in the variable $str.

Output of the above ltrim() function:

s is a test string!

rtrim()

The functionality and usage of rtrim() function is exactly similar to chop() function.

Related Posts
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