How to use sscanf in PHP

In this article I will explain how the sscanf() function can be used in PHP.
  • 1708

sscanf() function in PHP

  • The sscanf() function is used to parse input from a string according to a specified format string.
  • In sscanf() function, the data will be returned as an array, If only two parameters are passed. 
  • In sscanf() function, the data parsed are stored in them., if optional parameters are passed.

Syntax

sscanf(string,format,arg1,arg2,arg++)

Parameter

  • string string is required parameter. Determine for the string to be read.
  • format format is required parameter. Determine the string and how to format the variables in it.

    Possible format values:

    • %% - It's return a percent sign.
    • %b - It is represent binary number.
    • %c - The character according to the ASCII value.
    • %d - It is represent signed decimal number.
    • %e - It is represent scientific notation.
    • %u - It is represent unsigned decimal number.
    • %f - It is represent floating-point number.
    • %F - It is represent floating-point number.
    • %o - It is represent octal number.
    • %s - It is represent String.
    • %x - It is represent Hexadecimal number (lowercase letters).
    • %X - It is represent Hexadecimal number (uppercase letters).

    Additional format values. These are placed between the % and the letter.

    • + (Forces both + and - in front of numbers. By default, only negative numbers are marked)
    • ' (Specifies what to use as padding. Default is space. Must be used together with the width specifier)
    • - (Left-justifies the variable value)
    • [0-9] (Specifies the minimum width held of to the variable value)
    • .[0-9] (Specifies the number of decimal digits or maximum string length)
  • arg1 arg1 is optional parameter. The first variable to store data.
  • arg2 arg2 is optional parameter. The second variable to store data.
  • arg++ arg++ is optional parameter. The third, fourth, and so on, to store data.

Example

The following example show to how the sscanf() function can be used in PHP.

<html>

<body>

<h3 style="color: grey;">sscanf() function example in PHP</h3>

    <?php

    $input_value = 'Marks : English - 90 :: Hindi - 85';

    sscanf($input_value, 'Marks : English - %d :: Hindi - %d', $English, $Hindi);

    var_dump($English, $Hindi);

    ?>

</body>

</html>

 

Output

sscanf-php.gif
You may also want to read these related articles here
 
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.