PHP Strings Comparing, Searching and Replacing

Comparison is, perhaps, one of the most common operations performed on strings. At times, PHP’s type-juggling mechanisms also make it the most maddening— particularly because strings that can be interpreted as numbers are often transparently converted to their numeric equivalent. Consider, for example, the following code:

$string = '123aa';
if($string == 123) {
   // The string equals 123

You’d expect this comparison to return false, since the two operands are most definitely not the same. However, PHP first transparently converts the contents of $string to the integer 123, thus making the comparison true. Naturally, the best way to avoid this problemis to use the identity operator === whenever you are performing a comparison that could potentially lead to type-juggling problems.

In addition to comparison operators, you can also use the specialized functions strcmp() and strcasecmp() to match strings. These are identical, with the exception  that the former is  case-sensitive, while the latter is not. In both cases, a result of zero indicates that the two strings passed to the function are equal:

$str = "Hello World";
if (strcmp($str, "hello world") === 0) {
   // We won’t get here, because of case sensitivity
if (strcasecmp($str, "hello world") === 0) {
   // We will get here, because strcasecmp()
   // is case-insensitive

A further variant of strcasecmp(), strcasencmp() allows you to only test a given number of characters inside two strings. For example:

$s1 = 'abcd1234';
$s2 = 'abcd5678';
   // Compare the first four characters
echo strcasencmp($s1, $s2, 4);
You can also perform a comparison between portions of strings by using the substr_compare() function.

Simple Searching Functionality
PHP provides a number of very powerful search facilities whose functionality varies from the very simple (and correspondingly faster) to the very complex (and correspondingly slower).

The simplest way to search inside a string is to use the strpos() and strstr() families of functions. The former allows you to find the position of a substring (usually called the needle) inside a string (called the haystack). It returns either the numeric position of the needle’s first occurrencewithin the haystack, or false if a match could not be found. Here’s an example:

$haystack = "abcdefg";
$needle = 'abc';
if(strpos($haystack, $needle) !== false){
   echo 'Found';
Note that, because strings are zero-indexed, it is necessary to use the identity operators when
calling strpos() to ensure that a return value of zero—which indicatesthat the needle occurs
right at the beginning of the haystack—is not mistaken for a return value of false.

You can also specify an optional third parameter to strpos() to indicate that you want the search to start froma specific position within the haystack. For example:

$haystack = '123456123456';
$needle = '123';
echo strpos($haystack, $needle); // outputs 0
echo strpos($haystack, $needle, 1); // outputs 6

The strstr() function works similarly to strpos() in that it searches the haystack
for a needle. The only real difference is that this function returns the portion of the
haystack that starts with the needle instead of the latter's position:

$haystack = '123456';
$needle = '34';
echo strstr($haystack, $needle); // outputs 3456
In general, strstr() is slower than strpos()—therefore, you should use the latter if your only goal
is to determine whether a certain needle occurs inside the haystack. Also, note that you cannot 
force strstr() to start looking for the needle from a given location by passing a third parameter.

Both strpos() and strstr() are case sensitive and start looking for the needle from the beginning of the haystack. However, PHP provides variants that work in a caseinsensitive way or start looking for the needle from the end of the haystack. For example:

// Case-insensitive search
echo stripos('Hello World', 'hello'); // outputs zero
echo stristr('Hello My World', 'my'); // outputs "My World"
// Reverse search
echo strrpos('123123', '123'); // outputs 3

Matching Against aMask
You can use the strspan() function to match a string against a “whitelist” mask of allowed characters. This function returns the length of the initial segment of the string that contains any of the characters specified in the mask:

$string = '133445abcdef';
$mask = '12345';
echo strspn($string, $mask); // Outputs 6

Both strspn() and strcspn() accept two optional parameters that define the starting position and the length of the string to examine. For example:

$string = '1abc234';
$mask = 'abc';
echo strspn($string, $mask, 1, 4);

In the example above, strspn() will start examining the string from the second character (index 1), and continue for up to four characters—however, only the first three character it encounters satisfy the mask’s constraints and, therefore, the script outputs 3.

Simple Search and Replace Operations
Replacing portions of a string with a different substring is another very common task for PHP developers. Simple substitutions are performed using str_replace() (aswell as its case-insensitive variation, str_ireplace()) and substr_replace(). Here’s an example:

echo str_replace("World", "Reader", "Hello World");
echo str_ireplace("world", "Reader", "Hello World");

In both cases, the function takes three parameters: a needle, a replacement string and a haystack. PHP will attempt to look for the needle in the haystack (using either a case-sensitive or case-insensitive search algorithm) and substitute every single instance of the latter with the replacement string. Optionally, you can specify a third parameter, passed by reference, that the function fills, upon return, with the number of substitutions made:

$a = 0; // Initialize
str_replace('a', 'b', 'a1a1a1', $a);
echo $a; // outputs 3

If you need to search and replace more than one needle at a time, you can pass the first two arguments to str_replace() in the formof arrays:

echo str_replace(array("Hello", "World"), array("Bonjour", "Monde"), "Hello World");
echo str_replace(array("Hello", "World"), "Bye", "Hello World");

In the first example, the replacements are made based on array indexes—the first element of the search array is replaced by the first element of the replacement array, and the output is “Bonjour Monde”. In the second example, only the needle argument is an array; in this case, both search terms are replaced by the same string resulting in “Bye Bye”.

If you need to replace a portion of a needle of which you already know the starting and ending point, you can use substr_replace():

echo substr_replace("Hello World", "Reader", 6);
echo substr_replace("Canned tomatoes are good", "potatoes", 7, 8);

The third argument is our starting point—the space in the first example; the function replaces the contents of the string from here until the end of the string with the second argument passed to it, thus resulting in the output Hello Reader. You can also pass an optional fourth parameter to define the end of the substring that will be replaced (as shown in the second example, which outputs Canned potatoes are good).
Combining substr_replace() with strpos() can prove to be a powerful tool. For example:

$user = "";
$name = substr_replace($user, "", strpos($user, '@');
echo "Hello " . $name;

By using strpos() to locate the first occurrence of the @ symbol, we can replace the rest of the e-mail address with an empty string, leaving us with just the username, which we output in greeting.

Extracting Substrings
The very flexible and powerful substr() function allows you to extract a substring from a larger string. It takes three parameters: the string to be worked on, a starting index and an optional length. The starting index can be specified as either a positive integer (meaning the index of a character in the string starting from the beginning) or a negative integer (meaning the index of a character starting from the end). Here are a few simple examples:

$x = '1234567';
echo substr($x, 0, 3); // outputs 123
echo substr($x, 1, 1); // outputs 2
echo substr($x, -2); // outputs 67
echo substr($x, 1); // outputs 234567
echo substr($x, -2, 1); // outputs 6

Next Topic | Previous Topic

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

Leave a Comment

Your email address will not be published. Required fields are marked *