Search and Replacement
PHP offers a wide array of functions for searching and replacing text inside strings using both the traditional "match and replace" approach and a special system known as regular expressions, which we will examine later in this book.
The simplest form of search consists of looking for a substring inside a string. This task is usually performed through a call to strpos($haystack, $needle[, $start]), which returns false if $needle cannot be found inside $haystack, or returns the position of $needle's first character inside $haystack otherwise. If the integer parameter $start is specified, the search operation is performed starting from the character in $haystack whose position corresponds to the value of $start.
For example, the following script will return String found at position 22:
<?php $haystack = 'Three merry men and a bottle of wine'; $pos = strpos ($haystack, 'bottle'); if ($pos === false) echo "String not found\n"; else echo "String found at position $pos\n"; ?>
There is one very important detail to notice in the previous script. To determine whether the call to strpos() did indeed succeed and a match for the substring bottle was found inside $haystack, the value of $pos is compared to false using the type-checking operator ===. The reason for this is that the Boolean value false is equivalent to the integer value zero. However, strpos() will return zero if $needle is found starting from the first character of $haystack. Therefore, simply checking the return value of a call to strpos() using an expression like
if (!strpos ($haystack, $needle)) die ("Failure");
<?php $haystack = 'Three merry men'; $pos = strpos ($haystack, 'Three'); if (!$pos) echo "String not found\n"; else echo "String found at position $pos\n"; ?>
Although strpos() performs its search left-to-right, it is possible to start searching from the end of a string and move backward using the strrpos function. Unlike strpos(), however, strrpos() is able to search for only one character. If you specify a string with more than one character as the $needle parameter, only the first character will be considered.
As you can imagine, strpos() is case sensitive, so that, for example, it wouldn't have been able to find the word "three" in the preceding example.
Interestingly, there is no non-case-sensitive alternative to strpos(). However, PHP provides the strstr function, which offers a functionality that is similar to strpos() and provides a non-case-sensitive variant called stristr().
<?php $haystack = 'Three merry men'; $pos = strstr ($haystack, 'merry'); if (!$pos) echo "String not found\n"; else echo "String found: $pos\n"; ?>
PHP provides two main functions for performing simple search-and-replace operations. The first one is substr_replace, which can be used whenever you know the location of the substring that must be replaced and its length. For example:
<?php $haystack = 'Three merry men'; $newstr = substr_replace ($haystack, 'sad', 6, 5); echo "$newstr\n"; ?>
The preceding script will return Three sad men. The substr_replace function works essentially by cutting out the substring of $haystack delimited by the third (start) and optional fourth (length) parameters, and then replaces it with the string passed to it in the second parameter.
Naturally, you do not always have the luxury of knowing exactly where the substrings to be replaced are in your haystack string; indeed, there might be more than one string that needs to be replaced. In these cases, you can use the str_replace function, which combines the search capabilities of strstr() with the replace functionality of substr_replace.
The syntax of str_replace() is as follows:
str_replace ($search, $replace, $subject)
The function works by finding all the occurrences of $search inside $subject and replacing them with $replace.
<?php $haystack = 'Three merry men'; $newstr = str_replace ('merry', 'sad', $haystack); echo "$newstr\n"; ?>