PHP parse string into variable function sscanf () based on format string

Source: Internet
Author: User
Tags parse string

Instance

Parse A string:

<?php$str = "Age:30 weight:60kg"; sscanf ($str, "age:%d weight:%dkg", $age, $weight);//Show Types and Valuesvar_dump ($ Age, $weight);? >

The sscanf () function parses input from a string according to the specified format. The sscanf () function parses a string into a variable based on a format string.

If only two parameters are passed to the function, the data is returned as an array. Otherwise, if additional parameters are passed, the parsed data is stored in these parameters. If the number of delimiters is greater than the number of variables that contain them, an error occurs. However, if the number of delimiters is less than the number of variables that contain them, the extra variable contains NULL.

Related functions:

    • printf ()-Output formatted string

    • sprintf ()-writes a formatted string into a variable

Grammar

SSCANF (string,format,arg1,arg2,arg++)
Parameters Describe
String Necessary. Specifies the string to read.
Format Possible format values:

  • %-Returns a percent semicolon%

  • %c-ascii value corresponding to the word

  • %d-contains a decimal number with a sign (negative, 0, positive)

  • %e-Use lowercase scientific notation (for example, 1.2e+2)

  • %u-no sign of ten-in Number of units (greater than or equal to 0)

  • %f-floating point

  • %o-Eight decimal

  • %s-string

  • %x-16 Decimal ( Lowercase letters

  • % X-16 decimal digits (uppercase letters)

Additional format values. Must be placed between% and letter (for example,%.2f):

  • + (preceded by a number plus + or-to define The positive and negative nature of numbers. By default, only negative numbers are marked, positive numbers are not marked)

  • ' (specifies what to use as a fill, and the default is a space.) It must be used with the width of the specified device. For example:% ' x20s (using "X" as the fill))

  • -(left adjustment variable value)

  • [0-9] (minimum width of the specified variable value)

  • . [ 0-9] (Specify number of decimal digits or maximum string length)

Comment: If you use more than one of the above format values, they must be used in the order above and cannot be disrupted.

Arg1 Optional. The first variable that stores the data.
Arg2 Optional. A second variable that stores data.
arg++ Optional. A third to fourth variable that stores data. And so on

Technical details

return value: If only two parameters are passed to the function, the data is returned as an array. Otherwise, if additional parameters are passed, the parsed data is stored in these parameters. If the number of delimiters is greater than the number of variables that contain them, an error occurs. However, if the number of delimiters is less than the number of variables that contain them, the extra variable contains NULL.
PHP version: 4.0.1+

More examples

Example 1

Use the format value%s,%d, and%c:

<?php$str = "If you divide 4 by 2 ' ll get 2"; $format = sscanf ($str, "%s%s%s%d%s%d%s%s%c");p Rint_r ($format); >

SSCANF (): reads from a string into data that matches the specified format.

Prototype: int sscanf (const char *str,const char * format,........);

Description: sscanf () Converts the string of the parameter str according to the parameter format string and formats the data. The converted result is stored in the corresponding parameter.

Success returns the number of arguments, and failure returns 0.

Note: SSCANF is similar to scanf, which is used for input, except that the latter takes the keyboard (stdin) as the input source, the former with a fixed string as the input source.

As we all know, sscanf is a useful function to take out integers, floating-point numbers, and strings from a string. It is simple to use, especially for integers and floating-point numbers.

Here are a few frequently used examples to illustrate his usage, so that we can understand his usage in depth.

Example:

 #include <stdio.h> #include <stdlib.h> #include <string.h>      int main () {char str[100];      Usage One: takes the specified length of the string sscanf ("12345", "%4s", str);        printf ("Usage one \nstr =%s\n", str);      Usage two: Format time int year,month,day,hour,minute,second; SSCANF ("2013/02/13 14:55:34", "%d/%d/%d%d:%d:%d", &year,&month,&day,&hour,&minute,&second      );        printf ("Usage two \ntime =%d-%d-%d%d:%d:%d\n", Year,month,day,hour,minute,second);      Usage Three: Read into the string sscanf ("12345", "%s", str);        printf ("use three \nstr =%s\n", str); Usage four:%*d and%*s add an asterisk (*) to indicate that skipping this data is not read in.      (that is, do not read this data into the parameters) sscanf ("12345acc", "%*d%s", str);        printf ("usage four \nstr =%s\n", str); Usage Five: A string that takes the specified character.      As in the following example, take the string that was encountered with ' + '.      SSCANF ("12345+acc", "%[^+]", str);        printf ("Usage five \nstr =%s\n", str); Usage Six: The string to be taken to the specified character set.      As in the following example, take a string that encounters a lowercase letter.      SSCANF ("12345+acc121", "%[^a-z]", str);      printf ("usage six \nstr =%s\n", str);  return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.