13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Matching <strong>and</strong> Replacing Substrings with String Functions<br />

121<br />

The function strstr(), which is the most generic, can be used to find a string or<br />

character match within a longer string. In <strong>PHP</strong>, the strchr() function is exactly the<br />

same as strstr(), although its name implies that it is used to find a character in a string,<br />

similar to the C version of this function. In <strong>PHP</strong>, either of these functions can be used to<br />

find a string inside a string, including finding a string containing only a single character.<br />

The prototype for strstr() is as follows:<br />

string strstr(string haystack, string needle);<br />

You pass the function a haystack to be searched <strong>and</strong> a needle to be found. If an exact<br />

match of the needle is found, the function returns the haystack from the needle<br />

onward; otherwise, it returns false. If the needle occurs more than once, the returned<br />

string will start from the first occurrence of needle.<br />

For example, in the Smart Form application, you can decide where to send the email<br />

as follows:<br />

$toaddress = ‘feedback@example.com’; // the default value<br />

// Change the $toaddress if the criteria are met<br />

if (strstr($feedback, ‘shop’))<br />

$toaddress = ‘retail@example.com’;<br />

else if (strstr($feedback, ‘delivery’))<br />

$toaddress = ‘fulfillment@example.com’;<br />

else if (strstr($feedback, ‘bill’))<br />

$toaddress = ‘accounts@example.com’;<br />

This code checks for certain keywords in the feedback <strong>and</strong> sends the mail to the appropriate<br />

person. If, for example, the customer feedback reads “I still haven’t received delivery<br />

of my last order,” the string “delivery” will be detected <strong>and</strong> the feedback will be sent<br />

to fulfillment@example.com.<br />

There are two variants on strstr().The first variant is stristr(), which is nearly<br />

identical but is not case sensitive.This variation is useful for this application because the<br />

customer might type "delivery", "Delivery", "DELIVERY", or some other mixed-case<br />

variation.<br />

The second variant is strrchr(), which is again nearly identical, but returns the<br />

haystack from the last occurrence of the needle onward.<br />

Finding the Position of a Substring: strpos() <strong>and</strong> strrpos()<br />

The functions strpos() <strong>and</strong> strrpos() operate in a similar fashion to strstr(),<br />

except, instead of returning a substring, they return the numerical position of a needle<br />

within a haystack. Interestingly enough, the <strong>PHP</strong> manual recommends using<br />

strpos() instead of strstr() to check for the presence of a string within a string<br />

because it runs faster.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!