BLOGGER / ADVERTISER :: Function of string manipulation in PHP :: June :: 2009

BLOGGER / ADVERTISER

June 26, 2009

Function of string manipulation in PHP

Filed under: Uncategorized

<" alt="" border="0" />—[if gte mso 9]> < —[if !mso]> < ![endif]-->

emoticon1. 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

  

trimStrip 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

 

shuffleShuffle 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>
&nbsp;

Example

 

$fullname = "nicholas";

$letterposition = strpos($fullname,"h");

echo $letterposition;

 

?>

 

 

5. Splitting a line of text

 

splitSplit 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];

 

?>

 

emoticon 

Comments »

The URI to TrackBack this entry is: http://terrorblogger.blogsome.com/2009/06/26/function-of-string-manipulation-in-php/trackback/

No comments yet.

RSS feed for comments on this post.

Leave a comment

Line and paragraph breaks automatic, e-mail address never displayed, HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>



Anti-spam measure: please retype the above text into the box provided.

Get free blog up and running in minutes with Blogsome
Theme designed by Ian Main

Check Page Rank of any web site pages instantly:
This free page rank checking tool is powered by Page Rank Checker service