String are basically a group of characters stored in a variable.
In this chapter, we will be going through some important string functions. However , complete reference to a number of string function is here
It is used to print a string or any variable to the screen. It is not exactly a function, so parentheses can be omitted. We can print out a number of strings at a time using this.
<?php echo 'Hello World !!'; // used without parentheses echo ('Hello World !!'); // written with parentheses $var = 'This is a string variable'; echo $var; ?>
Explode function is used to divide a whole string using a delimiter. The function searches for the specified delimiter within the string passed and then splits the string into a number of strings . It returns an array of the resulting string.
<?php $myString = alpha~1gff~545~jhjhj~gadts~hjghh~tyffgy; $delimiter = "~"; $splitted_array = explode($delimiter,$myString); // The $splitted_array now contains an array of 7 elements and $splitted_array will return 545 ?>
This function takes a HTML content as argument and returns a converted string which can be used to write the same HTML content on the page. It is basically useful in preventing user-supplied text to show exactly what they supplied, if it contains any HTML markup, such as in a message board or guest book application.
<?php $new = htmlspecialchars(" <a href='test'>Test</a> "); echo $new ; ?>
This will print out the same content whatever was passed to it, without any modification. In above example, it will be shown as text and not HTML link.
The function returns the md5 hash of the string passed. This function is used mainly to convert user password into a format which is not decodable.
echo md5("Hello World");
The function takes two strings to be compared as argument and returns a value <0 if string1 is less than string2 and >0 if vice-versa. It returns 0 if both the strings are exactly same.
The comparison is such that it maches character wise from left to right till it gets first mis-matching character. It then decides according to the position of mismatching character.
<?php $new = strcmp('string1','string2'); echo $new ; ?>
It returns the length of string passed to the function.
echo strlen("abcde"); // returns 5
It takes a char to be searched in a given string and returns the position of the first occurence of character in the string. It returns FALSE if it doesn't find the character in the string passed.
echo strpos("abcde","b"); // returns 1
The function replaces all the occurrences of the search string with the replacement string provided. It behaves differently depending upon the type of argument passed to the function.
- If search and replace are both arrays , then it takes a value from search , finds it and replaces it with corresponding replace string available.
- If number of values available in replace is less than the values present in the search array, then it replaces the remaining with an empty string ""
- If search is an array and replace is a string, then it replaces each of the values in search with the only value available in replace string.
<?php $bodytag = str_replace("search", "replac", "I am searching"); // Returns: I am replacing $vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U"); $onlyconsonants = str_replace($vowels, "", "Hello World of PHP"); // Returns: Hll Wrld f PHP $phrase = "You should eat fruits, vegetables, and fiber every day."; $healthy = array("fruits", "vegetables", "fiber"); $yummy = array("pizza", "beer", "ice cream"); $newphrase = str_replace($healthy, $yummy, $phrase); // Returns: You should eat pizza, beer, and ice cream every day $str = str_replace("ll", "", "good golly miss molly!", $count); echo $count; // Returns: 2 ?>
After going through the above mentioned functions to handle and manipulate strings, lets have a look at some of the operators available in PHP