Home Tutorials IT Jobs Source Codes Certifications Discussion Forum
  PHP Tutorials
PHP Introduction
PHP Installation
Syntax
Variables
Echo
Strings
Operators
Comments
Require
Include
If...Else
Switch
Forms
Functions
Arrays
While Loop
For Loop
For Each
Do While
Post & Get
File Create
File Open
File Close
File Write / Read
File Delete
File Append
File Truncate
File upload
PHP str replace
PHP substr_replace
PHP Capitalization
PHP Explode
PHP Implode
   IT Jobs
Software Jobs
Networking Jobs
   Model Question Papers
BE Computer Science
MCA
BCA
Others
 
   

PHP Strings

The ability to manipulate string variables in PHP is extremely helpful. This tutorial will outline some of the more common situations you’ll encounter when working with PHP string variables.

Review: what’s a string?

In PHP, and every other flavor of Web programming, a string is a variable contained between quotes with a literal value. For example, $number = “2” is a string variable, whereas $number = 2 is not. The first value is seen as text only, the latter as a numeric value. Simply put, a string is a text variable.

Working with PHP strings: setting the stage

For this tutorial, we need a string variable to work with. Since my Red Sox were promptly ousted in last night’s MLB Playoffs, we’ll use them as my whipping boy. Here’s our string:


<?php
$string = "The Boston Red Sox lost because of poor pitching, bad defense, and a lack of offense. That doesn’t leave much, does it?";
?>


If we printed this variable (echo $string;) it would display:
The Boston Red Sox lost because of poor pitching, bad defense, and a lack of offense. That doesn’t leave much, does it?

Capitalize the first letter of every word in a PHP string

Let’s say we have a situation where we’re pulling information out of a database (maybe book titles or first names and last names) and we want to make sure the first letter of each word is capitalized. This is easily accomplished with the function ucwords().


<?php
$string ="The Boston Red Sox lost because of poor pitching, bad defense, and a lack of offense. That doesn’t leave much, does it?";
echo ucwords($string);
?>


This would print:
The Boston Red Sox Lost Because Of Poor Pitching, Bad Defense, And A Lack Of Offense. That Doesn’t Leave Much, Does It?


Capitalize every letter in a PHP string

<?php
$string = "The Boston Red Sox lost because of poor pitching, bad defense, and a lack of offense. That doesn’t leave much, does it?";
echo strtoupper($string);
?>


Prints:
THE BOSTON RED SOX LOST BECAUSE OF POOR PITCHING, BAD DEFENSE, AND LACK OF OFFENSE. THAT DOESN’T LEAVE MUCH, DOES IT?

 

Make every letter in a PHP string lowercase

Very similar to the above example, except we use the PHP function strtolower() instead of strtoupper().


<?php
$string = "The Boston Red Sox lost because of poor pitching, bad defense, and a lack of offense. That doesn’t leave much, does it?";
echo strtolower($string);
?>


Prints:
the boston red sox lost because of poor pitching, bad defense, and lack of offense. that doesn’t leave much, does it?


Replace parts of a PHP string

We can also replace part of our string with an entirely different piece of text. Consider it PHP’s built in “Find and Replace” function. This is accomplished via the str_replace() function.

<?php
$string = "The Boston Red Sox lost because of poor pitching, bad defense, and a lack of offense. That doesn’t leave much, does it?";
$newstring = str_replace("Boston Red Sox", "New York Yankees", $string );
echo $newstring;
?>


The str_replace() function takes 3 parameters. In my example, I used “Boston Red Sox,” “New York Yankees”, and $string. The first parameter is the piece of text you want to replace. The second is the text that will replace the original. The third parameter tells PHP what value to do the find and replace on (in this case, my original $string variable).

In the above example, echo $newstring will print:
The New York Yankees lost because of poor pitching, bad defense, and a lack of offense. That doesn’t leave much, does it?

After Randy Johnson’s stellar pitching performance last night, it turns out this might actually be true before the end of the weekend. Red Sox and Yankee fans unite!

Return all data AFTER a given character/string

This is a tricky technique. By using the PHP function strstr() I can return every part of the string after a given character or characters that I specify. For example:


<?php
$string = "The Boston Red Sox lost because of poor pitching, bad defense, and a lack of offense. That doesn’t leave much, does it?";
$newstring =strstr ($string, "Boston");
echo($newstring);
?>


I passed the strstr() function 2 parameters. The first is what variable to perform the string manipulation on($string). The second is the cutoff point in the text (take everything after and including this point).

The output of $newstring would be:
Boston Red Sox lost because of poor pitching, bad defense, and a lack of offense. That doesn’t leave much, does it?

I could also do:
$newstring =strstr ($string, "That");

Which would display:
That doesn’t leave much, does it?
 

Advanced: Return all data BEFORE a given character/string

I can also print all of the data that occurs before a certain character or string component occurs. This is an advanced technique that requires 2 functions: substr() and strpos(). Currently, my $string variable consists of two sentences. By using a clever combination of substr() and $strpos, I can cut off the entire second sentence, leaving me with only the first.


<?php
$string = "The Boston Red Sox lost because of poor pitching, bad defense, and a lack of offense. That doesn’t leave much, does it?";
$newstring = substr($string, 0, strpos($string, "."));
echo($newstring);
?>


This would print:
The Boston Red Sox lost because of poor pitching, bad defense, and a lack of offense

<< Previous | Next >>
Home  |  About us  | Privacy  |  Disclaimer  |  Contact us |  Advertise with us | Our Link Partners
All Rights Reserved 2009, CodeTeller.com