2013-02-16

Have you heard about 'Sscanf' function?

When I asked my boyfriend, when the function 'Sscanf' can be useful, he couldn't give me a good answer to this question. My boyfriend is a PHP programmer..
.. The fact is that this function is not well known because most of the complex codes can be written without the knowledge of the 'Sscanf'. But if such function exists in the PHP programming language, there's always the possibility that, when we will be reading someone's code, we can just come across at it. Therefore, it is good to know, what 'Sscanf' is. The syntax of the function looks like this:

                                 sscanf(string, format, arg1, arg2, arg3..)

 'Sscanf' function works very easy. It is essentially the inverse of 'Sprintf' function. The 'Sprintf' syntax looks like this:

                                 sprintf(format,arg1,arg2,arg3..)

'Sprintf' function writes a formatted string to a variable. While 'Sscanf' function parses input from a string according to a format, which means that 'Sscanf' reads input for numbers and other data types from standard input. We will understand it better on simple example:

                                 <?php
                                 $result1= sprintf("s% s%, d%",'January','1st','2013');
                                 $result2= sscanf("January 1st, 2013", 's%','s%','d%');

                                 echo $result1;
                                 print_r($result2);
                                 ?> 
RESULT1:                 RESULT2:
 January 1st, 2013                    Array(    
                                                           [0]=>January
                                                    [1]=>1st,
                                                       [2]=>2013)

So as you can see, we can use 'Sscanf' when we want to capture specific values with the variables. But of course if I'm wrong, I'm always open for correction.

Brak komentarzy:

Prześlij komentarz

Dziękuję Ci za poświęcony czas i pozostawienie komentarza :)