PHP - Searching a string for a matching set of characters

Using Code Igniter, there are many helper functions built-in that cause me to sometimes forget about the raw PHP alternatives.  One such function is STRPOS.

Its job is to return an integer that marks the string position of the first character match you feed into the function.

I discovered and implemented this function for the first time while setting up a Trulia Property feed for a client.  I noticed while setting up the feed that their current CMS was not saving zip code information which I knew had to be hurting them.  Rather than jump in and alter their CMS I simply went into the PHP code I wrote to generate the feed and added in the following:


if (  strpos($my_string, $characters) === FALSE ) {
    echo '';
} else {
    echo '08243';
}

What this code does is check the string for the match and return false if no match is found.  I’m not interested in the numeric position of the string I’m searching for, only if its there or not.  Easy enough!


About Joseph R. B. Taylor

Joseph R. B. Taylor is a humble designer/developer who makes stuff for screens of all shapes and sizes. He is currently the lead UI/UX Architect at MScience, LLC, where he works to create simple experiences on top of large rich datasets for their customers and clients.