From the course: PHP for Web Designers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Finding and extracting a substring

Finding and extracting a substring - PHP Tutorial

From the course: PHP for Web Designers

Start my 1-month free trial

Finding and extracting a substring

The plan for processing the data from the Build a Bouquet form in the Hansel and Petal site, involves identifying whether an array key begins with a known prefix, and stripping it off. Before diving into the code, let's take a look at how to find and extract a substring. In this exercise file, one of the array keys we'll be working with has been assigned to a variable called original. On line two. What we need to do is to find out whether it begins with color_, color underscore and if it does extract the final part, in this case calla lillies the name of the flower. So, to find the position of a substring within another string you use a function which has an impossible name. Strpos, which stands for string position. And it takes two arguments. The first one is the string that you want to search, and the second is the substring that you're looking for. So let's try that out. On line 14, I'm going to add a PHP block. And we want to display the results of strpos, so it will be echo and…

Contents