www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons PHP String Functions Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
There are many built in string manipulation functions available in PHP. We will discuss the most important and the most frequently used PHP string manipulation functions in this lesson.

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


Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.