Our article is about the PHP function sscanf(), which is used to parse input from a string. This function is useful for extracting data from strings that have a consistent format. In this article, we will discuss the syntax and usage of sscanf(), as well as provide some examples.

The sscanf() function is used to parse input from a string. The syntax of the sscanf() function is as follows:

int sscanf ( string $str , string $format , mixed &$... )

The function takes two required parameters: $str and $format. The $str parameter is the string to be parsed, and the $format parameter is a string that specifies the format of the input.

Here is an example of how to use the sscanf() function:

<?php
$input = 'John 25';
sscanf($input, '%s %d', $name, $age);
echo $name . "\n"; // Output: John
echo $age; // Output: 25
?>

In this example, we have a string variable $input that we want to extract data from. We use the sscanf() function to extract the name and age from the string.

The output of this code will be:

John
25

As you can see, the sscanf() function has extracted the name and age from the string.

The sscanf() function is a useful tool for parsing input from a string. It allows you to extract data from strings that have a consistent format. By mastering this function, you can become a more proficient PHP developer.

We hope this article has been helpful in understanding the sscanf() function in PHP.


Do you find this helpful?