PHP - string manipulation

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

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
Firstly, we need a string variable to work with. Here’s our string:


<?php
$string = "Let his Holiness Swami Baktanimbin relieve you of the heavy burden of your wallet. and the bad kharma of your credit card details.";
?>


If we printed this variable (echo $string;) it would display:
Let his Holiness Swami Baktanimbin relieve you of the heavy burden of your wallet. and the bad kharma of your credit card details.

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 ="Let His Holiness Swami Baktanimbin Relieve You Of The Heavy Burden Of Your Wallet. And The Bad Kharma Of Your Credit Card Details.";
echo ucwords($string);
?>


This would print:
Let His Holiness Swami Baktanimbin Relieve You Of The Heavy Burden Of Your Wallet. And The Bad Kharma Of Your Credit Card Details.

Capitalize every letter in a PHP string

<?php
$string = "Let his Holiness Swami Baktanimbin relieve you of the heavy burden of your wallet. and the bad kharma of your credit card details.";
echo strtoupper($string);
?>


Prints:
LET HIS HOLINESS SWAMI BAKTANIMBIN RELIEVE YOU OF THE HEAVY BURDEN OF YOUR WALLET. AND THE BAD KHARMA OF YOUR CREDIT CARD DETAILS.


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 = "Let his Holiness Swami Baktanimbin relieve you of the heavy burden of your wallet. and the bad kharma of your credit card details.";
echo strtolower($string);
?>


Prints:
let his holiness swami baktanimbin relieve you of the heavy burden of your wallet. and the bad kharma of your credit card details.


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 = "Let his Holiness Swami Baktanimbin relieve you of the heavy burden of your wallet. and the bad kharma of your credit card details.";
$newstring = str_replace("Baktanimbin", "Chanjindapokit Baktanimbin", $string );
echo $newstring;
?>


The str_replace() function takes 3 parameters. In my example, I used “Baktanimbin” “Chanjindapokit Baktanimbin”, 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:
Let his Holiness Swami Chanjindapokit Baktanimbin relieve you of the heavy burden of your wallet. and the bad kharma of your credit card details.

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 = "Let his Holiness Swami Baktanimbin relieve you of the heavy burden of your wallet. and the bad kharma of your credit card details.";
$newstring =strstr ($string, "and");
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:
and the bad kharma of your credit card details.