Function of string manipulation in PHP
<" alt="" border="0" />—[if gte mso 9]>—[if !mso]> < ![endif]-->
1. Changing Case
The ability take strings of text and manipulate them is one of the essential abilities you need as a programmer. If a user enters details on your forms, then you need to check and validate this data. For the most part, this will involve doing things to text. Examples are: converting letters to uppercase or lowercase, checking an email address to see if all the parts are there, checking which browser the user has, trimming white space from around text entered in a text box. All of these come under the heading of string manipulation. To make a start, we’ll look at changing the case of character.
Example:
$fullname = ‘nicholas cage’;
$fullname = ucwords($fullname);
echo $fullname;
// Use strtolower and strtoupper functions too
?>
2. Trimming White Space
trim — Strip whitespace (or other characters) from the beginning and end of a string
This function returns a string with whitespace stripped from the beginning and end of str . Without the second parameter, trim() will strip these characters
Example
trim(" Test "); // Remove white space from both sides of a string
ltrim(" Test "); // Remove white space from left side of a string
rtrim(" Test "); // Remove white space from right side of a string
?>
3. Shuffle characters
shuffle — Shuffle an array
This function shuffles (randomizes the order of the elements in) an array.
Example:
$fullname = ‘nicholas cage’;
$fullname = strshuffle($fullname);
echo $fullname;
?>
4. Finding String Positions with strpos
<span>Although strpos returns the position of the needle correctly, when</span>
<span>testing the return value with a boolean operator to see if it's true, it</span>
<span>produces the opposite result.</span>
Example
$fullname = "nicholas";
$letterposition = strpos($fullname,"h");
echo $letterposition;
?>
5. Splitting a line of text
split — Split string into array by regular expression
array split ( string $pattern , string $string [, int $limit ] )
Splits a string into array by regular expression.
Example:
$fullname = "Jaydra,Joe,Elizabeth,Abby,Vilma,Laura";
$names = explode(",",$full_name); // Returns an array by breaking string with "," sign
echo $names[0];
?>



