You may not know this, but the latest version of PHP comes with avery powerful set of string manipulatation tools. This article takes anin-depth look at these tools and illustrates how they can save you time andeffort in your daily development activities.
With the basics out of the way, let's now turn to some of the other string functions available in PHP. Other than echo() and print(), the three functions you're likely to encounter most often are strlen(), explode() and implode().
The strlen() function returns the length of a particular string, and can come in handy for operations which involve processing every character in a string.
<?
$str = "The wild blue fox jumped over the gigantic yellow pumpkin";
// returns 57
echo strlen($str);
?>
The explode() function splits a string into smaller
components on the basis of a user-specified pattern, and then returns these elements as an array.
<?
$str = "I'm not as think as you stoned I am";
// split into individual words and store in $words[]
$words = explode(" ", $str);
?>
This function is particularly handy if you need to take a
string containing a list of items (for example, a comma-delimited list) and separate each element of the list for further processing. Here's an example:
<?
$str = "Rachel, Monica, Phoebe, Joey, Chandler, Ross";
// split into array
$arr = explode(", ", $str);
// display as list
echo "<ul>";
foreach($arr as $friend)
{
echo "<li>$friend";
}
echo "</ul>";
?>
Obviously, you can also do the reverse - the implode()
function creates a single string from all the elements of an array, joining them together with a user-defined separator. Reversing the example above, we have:
<?
$arr = array("Rachel", "Monica", "Phoebe", "Joey", "Chandler", "Ross");
// create string from array
$str = implode(" and ", $arr);
// returns "Rachel and Monica and Phoebe and Joey and Chandler and Ross are
friends"
echo "$str are friends";
?>
The chr() and ord() functions come in handy when converting
from ASCII codes to characters and vice-versa. For example,
In case you need to repeat a string, PHP offers the
str_repeat() function, which accepts two arguments - the string to be repeated, and the number of times to repeat it. Here's an example:
<?
// returns "Play it again, Sam!" eight times
echo str_repeat("Play it again, Sam!", 8);
?>
And if you ever find the need to reverse a string, well, you
can always reach for the strrev() function...
<?
$str = "Wassup, dood?";
// reverse string
// $rts now contains ?dood ,pussaW
$rts = strrev($str);
echo "Sorry, you seem to be talking backwards - what does $rts mean?";
?>
I couldn't have put it better myself!{mospagebreak title=Of
Jumping Cows And Purple Pumpkins} Next up, the substr() function. As the name implies, this is the function that allows you to slice and dice strings into smaller strings. Here's what it looks like:
substr(string, start, length)
where "string" is a string or string variable, "start" is the
position to begin slicing at, and "length" is the number of characters to return from "start".
Here's an example which demonstrates how this works:
<?
$str = "The cow jumped over the moon, giggling madly as a purple pumpkin
with fat ears exploded into confetti";
// returns "purple pumpkin with fat ears"
echo substr($str, 50, 28);
?>
You can use this function to split a string into smaller
chunks of a fixed size,
<?
$str = "The cow jumped over the moon, giggling madly as a purple pumpkin
with fat ears exploded into confetti";
$count = 0;
// length of chunk
$size = 11;
/* returns
The cow jum
ped over th
e moon, gig
gling madly
as a purpl
e pumpkin w
ith fat ear
s exploded
into confet
ti
*/
while (($size*$count) < strlen($str))
{
$temp = substr($str, ($size*$count), $size);
$count++;
echo "$temp \n";
}
?>
or you could take the easy way out and use the built-in
chunk_split() function, designed specifically for this purpose.
<?
$str = "The cow jumped over the moon, giggling madly as a purple pumpkin
with fat ears exploded into confetti";
// this is equivalent to the previous example
echo chunk_split($str, 11);
?>
You can also use the substr() function to extract a
particular character from a string,
<?
$str = "The cow jumped over the moon, giggling madly as a purple pumpkin
with fat ears exploded into confetti";
// returns "j"
echo substr($str, 8, 1);
?>
or you can use one of PHP4's cool new features and access a
character by specifying its position in the string within curly braces (remember that the first character equates to position 0).
<?
$str = "The cow jumped over the moon, giggling madly as a purple pumpkin
with fat ears exploded into confetti";
// returns "j"
echo $str{8};
?>