Most Important String Functions in PHP a programmer should know :

1) substr()

This function returns the part of the string as an output.

<?php echo substr("Hello world",6); ?> //Returns world


2) strlen()

This function returns the length of the string.

<?php echo strlen("Hello world");  ?> // Returns 11


3) trim()

This function removes the whitespaces from both start and the end of the string.

<?php echo trim( "        Hello World    "); ?>  // returns Hello World. If you go view source then you can see that there are no whitespaces.


4) ltrim()

This function removes the whitespaces from the left part of the string.

<?php echo ltrim( "        Hello World    "); ?>  // returns Hello World. If you go view source then you can see that there are no whitespaces on left side but there are spaces on right side.


5) rtrim()

This function removes the whitespaces from the right part of the string.

<?php echo rtrim( "        Hello World    "); ?>  // returns Hello World. If you go view source then you can see that there are no whitespaces on right side but there are spaces on left side


6) strtolower()

This function converts the string to lower case.

<?php echo strtolower("HELLO WORLD"); ?> // Returns hello world


7) strtoupper()

This function converts the string to upper case.

<?php echo strtoupper("hello world"); ?> // Returns HELLO WORLD


8) str_replace()

The str_replace() function replaces some characters with some other characters in a string.

<?php echo str_replace("world","Peter","Hello world"); ?>// Returns Hello Peter


9) strcmp()

The strcmp() function compares two strings.

<?php echo strcmp("Hello world!","Hello world!"); ?> //Returns 0


10) explode()

This function breaks the string into array on the basis of delimiter passed.

<?php
 $str = "Hello world. Its a beautiful day.";
 print_r (explode(" ",$str));
 ?>

/* Output :

Array
 (
 [0] => Hello
 [1] => world.
 [2] => Its
 [3] => a
 [4] => beautiful
 [5] => day.
 )

*/


11) implode()

This function join array elements with a string on the basis of delimiter passed.

<?php
 $arr = array("Hello","World!","Beautiful","Day!");
 echo implode(" ",$arr);
 ?>

/*

Output:

Hello World! Beautiful Day!

*/