![]() |
|
PHP String Functions. Free online PHP and MySQL Web Database Programming Tutorial... |
| Lessons | PHP String Functions |
|
|
addslashes() and stripslashes() The addslashes() and stripslashes() functions are very important when we need to inster or retrieve data from a database. It is very common to have special characters in our text like single quotes, double quotes, backslashes, or NULL. Before the insertion of such text in the database these special characters have to be escaped. The PHP function addslashes() does just that, very efficiently. The syntax follows: addslashes(string); The stripslashes() function reverses the operation performed by the addslashes() function i.e. it returns the argument string with the slashes taken away. The syntax follows: stripslashes(string); ltrim(), chop(), and trim() All three of these functions are used to remove white space from a string. The function ltrim() removes white space from the beginning of the argument string. The chop() function removes white space from the end of the argument string, and the trim() function removes white space from both sides i.e. from the beginning and from the end of the argument string as well. Let's see the syntax of these functions: ltrim(string); chop(string); trim(string); echo() The echo() function is used to print or return the argument string. The syntax of echo() follows: echo ("string 1", "string 2", ...) explode() and implode() The explode() function splits the argument string using a given separator and returns the values in an array. The syntax of explode() follows: explode("separator", "string"); Example $fruitArray: $fruits = "banana, orange, apple, mango"; $fruitArray = explode(",", $fruits); The above code takes a string called $fruits, containing a comma-separated list of fruits, and places each fruit into an array called $fruitArray. On the other hand, the implode() function takes an array and turns it into a single string, using a given separator. The syntax of implode() follows: implode("separator", "stringArray"); For example, the following code takes an array called $fruitsArray and then creates a string called $fruits, containing the values of the $fruitsArray array, separated by commas: $fruits = implode("," , $fruitsArray); strlen() The strlen() function returns the length of a given string. Its syntax follows: strlen(string); strtolower() The strtolower() function returns a given string with all alphabetic characters in lowercase. Its syntax follows: strtolower(str); strtoupper() The strtoupper() function returns a given string with all alphabetic characters in uppercase. Its syntax follows: strtoupper (str); substr() The substr() function returns a portion of the argument string, given a starting position and the optional length argument. Its syntax follows: substr(string, start, [length]); If the start position is a positive number, the starting position is counted from the beginning of the string. If the start position is negative, the starting position is counted from the end of the string. Similarly, if the optional length parameter is used and is a positive number, the length is counted from the beginning of the string. If the length parameter is used and is a negative number, the length is counted from the end of the string. Let's see some examples: $new_string = substr("PHP is awsome!", 1); // returns "HP is awsome!" $new_string = substr("PHP is awsome!", 0, 7); // returns "PHP is " $new_string = substr("PHP is awsome!", -1); // returns "!" $new_string = substr("PHP is awsome!", -6, 5); // returns "awsome" ucfirst() The ucfirst() function changes the first alphabetic character of the argument string to an uppercase character. Its syntax follows: ucfirst(string); ucwords() The ucwords() function changes the first letter of each word of the argument string to uppercase. Its syntax follows: ucwords(string);
Next: PHP Arrays
|