作为程序员一定要保持良好的睡眠,才能好编程

PHP sscanf 的作用以及用法

发布时间:2018-04-03


分割字符串函数:

<?php

$qs = "index.php?id=34&name=john&j=333";

print_r(sscanf($qs, "%[^?]?%[^?]"));

/**
返回结果:

Array
(
    [0] => index.php
    [1] => id=34&name=john&j=333
)

解释说明 所有的分割符以% 百分号开头 

*/



$qs = "id=34&name=john&bb=c";

print_r(sscanf($qs,"id=%[^&]&name=%[^&]"));

/**
返回结果
Array
(
    [0] => 34
    [1] => john
)

返回数组,可以使用 list($id,$name)=sscanf($qs,"id=%[^&]&name=%[^&]");

接收这里的两个参数,
使用 $id,$name 直接进行使用。

写几个百分号就返回几个数据。

但是如果没有匹配到数据以 NULL 返回  NULL 就是 false


例如:

---------------------------------------------------
不能以c开头,因此匹配不到,返回NULL
---------------------------------------------------
$qs = "id=34&name=john&bb=c";

var_dump(sscanf($qs,"id=%[^&]&name=%[^&]&bb=%[^c&]"));
array(3) {
  [0]=>
  string(2) "34"
  [1]=>
  string(4) "john"
  [2]=>
  NULL
}

---------------------------------------------------
换一个不以c开头的,可以匹配到  ba
---------------------------------------------------
$qs = "id=34&name=john&bb=ba";

var_dump(sscanf($qs,"id=%[^&]&name=%[^&]&bb=%[^c&]"));
array(3) {
  [0]=>
  string(2) "34"
  [1]=>
  string(4) "john"
  [2]=>
  string(2) "ba"
}

---------------------------------------------------
不以c开头 则匹配到a&age=33
---------------------------------------------------
$qs = "id=34&name=john&bb=a&age=33";

var_dump(sscanf($qs,"id=%[^&]&name=%[^&]&bb=%[^c]"));
array(3) {
  [0]=>
  string(2) "34"
  [1]=>
  string(4) "john"
  [2]=>
  string(1) "a&age=33"
}




*/


sscanf 的分割符号 %s  %d 





function formatPhone($phone) {
       if (empty($phone)) return "";
       if (strlen($phone) == 7)
               sscanf($phone, "%3s%4s", $prefix, $exchange);
       else if (strlen($phone) == 10)
               sscanf($phone, "%3s%3s%4s", $area, $prefix, $exchange);
       else if (strlen($phone) > 10)
               if(substr($phone,0,1)=='1') {
                                 sscanf($phone, "%1s%3s%3s%4s", $country, $area, $prefix, $exchange);
                             }
                             else{
                                 sscanf($phone, "%3s%3s%4s%s", $area, $prefix, $exchange, $extension);
                                }
       else
               return "unknown phone format: $phone";
       $out = "";
       $out .= isset($country) ? $country.' ' : '';
       $out .= isset($area) ? '(' . $area . ') ' : '';
       $out .= $prefix . '-' . $exchange;
       $out .= isset($extension) ? ' x' . $extension : '';
       return $out;
}


/*function formatMoney($input, $decimals = 2) {
  is_numeric($input) || $input = 0;
  if (is_string($input) && stripos($input, 'E') === FALSE) {
    //sscanf($input, '%[^.].%s', $integer, $decimal);
    sscanf($input, '%[^.].%s');
    

     

    $result = '';
    $integer = strrev($integer);
    for ($i = strlen($integer) - 1; $i >= 0; $i--) {
      $result .= $integer[$i] . ($i % 3 === 0 && $i !== 0 ? ',' : '');
    }

    $decimal = strlen($decimal) < $decimals ? str_pad($decimal, $decimals, '0') :
      substr(sprintf("%.{$decimals}f", '.' . $decimal), 2);

    return $result . '.' . $decimal;
  }

  return number_format($input, $decimals);
}*/


$out = sscanf('file_name.gif', '%s_%s.%s', $fpart1,$fpart2,$fpart3);


  //print_r($out);
 print_r($fpart1);
 print_r($fpart2);

a3.png




fscanf() 方法的使用

$handle = fopen("users.txt","r");
while ($userinfo = fscanf($handle, "%s\t%s\t%s\n")) {
    list 
($name, $profession, $countrycode) = $userinfo;
    //... do something with the values
}
fclose($handle);



users.txt 
javier  argonaut        pe
hiroshi sculptor        jp
robert  slacker us
luigi   florist it




printf() 函数是做什么的?  输出格式化字符串

其他话不多说,好像是预编译

看代码吧

$num=100;
$location="beijing";
$format = "There are %d monkeys in the %s";
printf($format, $num, $location);

//预览结果:There are 100 monkeys in the beijing

/**功能类似于 在语句中使用嵌套变量是一个道理。

   使用printf 参数必须一一对应
   
   否则就会出现这个问题:
   $num=100;
   $location="beijing";
   $format = "The %s contains %d monkeys";
   printf($format, $num, $location);
   The 100 contains 0 monkeys
   第二个参数本来是数字的,我传递了一个字符串 系统给转换成了0
      
   
   可以指定参数的顺序
   
   $format = "The %2\$s contains %1\$d monkeys";
      printf($format, $num, $location);
      
      %s => %2\$s  指定成第二个参数解析
      %d => %1\$d
      
      进行解析


 
   
   
      
      

*/




 $num=100.99;


 printf("%.2f",$num);  //直接输出

 echo sprintf("%.2f",$num); //返回到一个变量中 


 echo number_format($num,2);  //printf 和 num_format(); 一样使用。