PHP String Functions Part 1
This post explains with examples some PHP string functions, there are many functions to deal with string, I will explain them all partly.
Here are some of PHP String Functions Examples
str_replace()
Replace all occurrences of the search string with the replacement string.
$string = 'Hello World';
echo str_replace("Hello", "Hi", $string);
// result : Hi World
substr()
Return part of a string
$string = 'Hello World'; echo substr($string, 0, 3); // result : Hel echo substr($string, -1); // result : d echo substr($string, 0, -1); // result : Hello Worl
- the first parameter is the string that we want to modify
- the second parameter is the start point of modification (when we use a negative value it will start from the end of string)
- the third parameter is the length of the modified string (when we use a negative value it will ignore a number of characters from the end of string)
trim()
Strip whitespace (or other characters) from the beginning and end of a string
$string = ' Hello World '; echo trim($string); // result : Hello World
str_split()
Convert a string to an array
$string = 'Hello World';
$array = str_split($string);
print_r($array);
/* will result
Array
(
[0]
=> H
[1]
=> e
[2]
=> l
[3]
=> l
[4]
=> o
[5]
=>
[6]
=> W
[7]
=> o
[8]
=> r
[9]
=> l
[10]
=> d
)
*/
/* we can add a second parameter to the function
which define the length of the letters in each chunk */
$array = str_split($string,3);
print_r($array);
/* will result
Array
(
[0]
=> Hel
[1]
=> lo
[2]
=> Wor
[3]
=> ld
)
*/
strlen()
Get string length
$string = 'Hello World'; echo strlen($string); // result : 11 (counts the whitespace also)
explode()
Split a string by string
$string = 'Hello World';
$array = explode(' ',$string); // explode the string by whitespace
/* result :
Array
(
[0]
=> Hello
[1]
=> World
)
*/
implode()
Join array elements with a string
$array = array('Hello','World');
echo implode(' ',$string); // implode the array by whitespace
// result : Hello World


![JQuery Functions [wrap-wrapInner-wrapAll-unwrap]](http://pluscss.com/wp-content/uploads/2014/05/265913821198741-150x150.jpg)
![JQuery Functions [html-append-prepend]](http://pluscss.com/wp-content/uploads/2014/05/97413821199241-150x150.jpg)
![Comments Scripts [the best of codecanyon]](http://pluscss.com/wp-content/uploads/2014/05/157713818605861-150x150.jpg)


