From the course: PHP Tips, Tricks, and Techniques

Unlock this course with a free trial

Join today to access over 22,400 courses taught by industry experts.

Extract values with a format string

Extract values with a format string

From the course: PHP Tips, Tricks, and Techniques

Extract values with a format string

- [David] Hi, I'm David Powers, and welcome to this week's edition of PHP Tips, Tricks, and Techniques designed to help you become a smarter, more productive PHP developer. This week, I'm going to look at extracting values with a format string. Let's start with the problem. You need to extract values from a string but you're not an expert at regular expressions. Well, there is a solution. Use sscanf with a format string. Although this function isn't widely known, it's part of a family you're probably familiar with. It's closely related to sprintf, which inserts values into a format string using conversion specifications. For example, you might have seen something like this. The first argument of sprintf is a format string containing two conversion specifications that'll be replaced by the values in price and tip. The conversion specifications begin with a percentage sign. In this example, the first one represents a floating point number to two decimal places and the second one is a…

Contents