GVKun编程网logo

php基于str_pad实现卡号不足位数自动补0的方法

15

这篇文章主要围绕php基于str_pad实现卡号不足位数自动补0的方法展开,旨在为您提供一份详细的参考资料。我们将全面介绍php基于str_pad实现卡号不足位数自动补0的方法,同时也会为您带来C#S

这篇文章主要围绕php基于str_pad实现卡号不足位数自动补0的方法展开,旨在为您提供一份详细的参考资料。我们将全面介绍php基于str_pad实现卡号不足位数自动补0的方法,同时也会为您带来C# String 前面不足位数补零的方法、Java Android 开发数字不足位数前面补0、JS中的PadLeft、PadRight,位数不足,自动补位,String扩展方法、Laravel实现构造函数自动依赖注入的方法_PHP的实用方法。

本文目录一览:

php基于str_pad实现卡号不足位数自动补0的方法

php基于str_pad实现卡号不足位数自动补0的方法

《:PHP基于str_pad实现卡号不足位数自动补0的方法》要点:
本文介绍了:PHP基于str_pad实现卡号不足位数自动补0的方法,希望对您有用。如果有疑问,可以联系我们。

自动补足空白位数在PHP中str_pad函数可以帮我们实现哦,str_pad() 函数把字符串填充为指定的长度.PHP实例

str_pad() 函数把字符串填充为指定的长度.
语法
str_pad(string,length,pad_string,pad_type)

PHP实例

参数 描述 string 必需.规定要填充的字符串. length 必需.规定新字符串的长度.如果该值小于原始字符串的长度,则不进行任何操作. pad_string 可选.规定供填充使用的字符串.默认是空白. pad_type

可选.规定填充字符串的那边.PHP实例

可能的值:PHP实例

  • STR_PAD_BOTH - 填充到字符串的两头.如果不是偶数,则右侧获得额外的填充.
  • STR_PAD_LEFT - 填充到字符串的左侧.
  • STR_PAD_RIGHT - 填充到字符串的右侧.这是默认的.

示例如下:
代码如下:
$cardCount = 10;
$arr = array();
for ($i = 1; $i <= $cardCount; $i++) {
$strCard = str_pad($i,10,'0',STR_PAD_LEFT);
$arr[] = $strCard;
}
print_r($arr);

运行后输出结果如下:
代码如下:

Array ( [0] => 0000000001 [1] => 0000000002 [2] => 0000000003 [3] => 0000000004 [4] => 0000000005 [5] => 0000000006 [6] => 0000000007 [7] => 0000000008 [8] => 0000000009 [9] => 0000000010 )

希望本文所述对大家的PHP程序设计有所赞助.PHP实例

《:PHP基于str_pad实现卡号不足位数自动补0的方法》是否对您有启发,欢迎查看更多与《:PHP基于str_pad实现卡号不足位数自动补0的方法》相关教程,学精学透。小编 jb51.cc为您提供精彩教程。

C# String 前面不足位数补零的方法

C# String 前面不足位数补零的方法

int i=10;
方法1:Console.WriteLine(i.ToString("D5"));
方法2:Console.WriteLine(i.ToString().PadLeft(5,''0''));//推荐
方法3:Console.WriteLine(i.ToString("00000")); 


在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位。

PadLeft(int totalWidth, char paddingChar) //在字符串左边用 paddingChar 补足 totalWidth 长度
PadLeft(int totalWidth, char paddingChar) //在字符串右边用 paddingChar 补足 totalWidth 长度
示例:
h = h.PadLeft(2, ''0'');
注意第二个参数为 char 类型,所以用单引号,也可以用 Convert.ToChar(string value) 把字符串转换成 char 类型。如果字符串长度大于 1,则使用 str.ToCharArray()[index]。  

Java Android 开发数字不足位数前面补0

Java Android 开发数字不足位数前面补0

import java.text.DecimalFormat;
public void changeColor(View view) {
        DecimalFormat decimalFormat = new DecimalFormat("000");
        //获取随机数对象,产生三个随机数值(RGB值)
        Random x = new Random();

        int red = x.nextInt(256);
        String sred = decimalFormat.format(red);
        txvR.setText("红:"+sred);
        txvR.setTextColor(Color.rgb(red,0,0));

        int green = x.nextInt(256);
        String sgreen = decimalFormat.format(green);
        txvG.setText("绿:"+sgreen);
        txvG.setTextColor(Color.rgb(0,green,0));

        int blue = x.nextInt(256);
        String sblue = decimalFormat.format(blue);
        txvB.setText("蓝:"+sblue);
        txvB.setTextColor(Color.rgb(0,0,blue));

        //设置界面最上方的按钮 button 的文字颜色
        button.setTextColor(Color.rgb(red,green,blue));

        //设置界面最下方的空白 LinearLayout 的背景颜色
        colorBlock.setBackgroundColor(Color.rgb(red,green,blue));
    }
import java.text.DecimalFormat;

//如果数字1是字符串,如下处理:
String string="1";
DecimalFormat decimalFormat =new DecimalFormat("0000");
String string2=decimalFormat.format(Integer.parseInt(str1));
System.out.println(string2);

//如果数字1是整型,如下处理:
int string=1;
DecimalFormat decimalFormat =new DecimalFormat("0000");
String string2=decimalFormat.format(string);
System.out.println(string2);

 

JS中的PadLeft、PadRight,位数不足,自动补位,String扩展方法

JS中的PadLeft、PadRight,位数不足,自动补位,String扩展方法

类似C#中的 PadLeft、PadRight方法

//方法一
    function FillZero(p) {
        return new Array(3 - (p + '''').length + 1).join(''0'') + p;
    }
    FillZero(6);    //输出006
 
    //方法一扩展(C#中PadLeft、PadRight)
    String.prototype.PadLeft = function (len, charStr) {
        var s = this + '''';
        return new Array(len - s.length + 1).join(charStr,  '''') + s;
    }
    String.prototype.PadRight = function (len, charStr) {
        var s = this + '''';
        return s + new Array(len - s.length + 1).join(charStr,  '''');
    }
    var p = 6;
    p.toString().PadLeft(3, ''0'');    //输出006
    p.toString().PadRight(3, ''0'');  //输出600

 

Laravel实现构造函数自动依赖注入的方法_PHP

Laravel实现构造函数自动依赖注入的方法_PHP

本文实例讲述了laravel实现构造函数自动依赖注入的方法。分享给大家供大家参考,具体如下:

在Laravel的构造函数中可以实现自动依赖注入,而不需要实例化之前先实例化需要的类,如代码所示:

<&#63;php
namespace Lio\Http\Controllers\Forum;
use Lio\Forum\Replies\ReplyRepository;
use Lio\Forum\Threads\ThreadCreator;
use Lio\Forum\Threads\ThreadCreatorListener;
use Lio\Forum\Threads\ThreadDeleterListener;
use Lio\Forum\Threads\ThreadForm;
use Lio\Forum\Threads\ThreadRepository;
use Lio\Forum\Threads\ThreadUpdaterListener;
use Lio\Http\Controllers\Controller;
use Lio\Tags\TagRepository;
class ForumThreadsController extends Controller implements ThreadCreatorListener, ThreadUpdaterListener, ThreadDeleterListener
{
 protected $threads;
 protected $tags;
 protected $currentSection;
 protected $threadCreator;
 public function __construct(
  ThreadRepository $threads,
  ReplyRepository $replies,
  TagRepository $tags,
  ThreadCreator $threadCreator
 ) {
  $this->threads = $threads;
  $this->tags = $tags;
  $this->threadCreator = $threadCreator;
  $this->replies = $replies;
 }
}

登录后复制

注意构造函数中的几个类型约束,其实并没有地方实例化这个Controller并把这几个类型的参数传进去,Laravel会自动检测类的构造函数中的类型约束参数,并自动识别是否初始化并传入。

源码vendor/illuminate/container/Container.php中的build方法:

$constructor = $reflector->getConstructor();
dump($constructor);

登录后复制

这里会解析类的构造函数,在这里打印看:

立即学习“PHP免费学习笔记(深入)”;

点击下载“修复打印机驱动工具”;

它会找出构造函数的参数,再看完整的build方法进行的操作:

public function build($concrete, array $parameters = [])
{
 // If the concrete type is actually a Closure, we will just execute it and
 // hand back the results of the functions, which allows functions to be
 // used as resolvers for more fine-tuned resolution of these objects.
 if ($concrete instanceof Closure) {
  return $concrete($this, $parameters);
 }
 $reflector = new ReflectionClass($concrete);
 // If the type is not instantiable, the developer is attempting to resolve
 // an abstract type such as an Interface of Abstract Class and there is
 // no binding registered for the abstractions so we need to bail out.
 if (! $reflector->isInstantiable()) {
  $message = "Target [$concrete] is not instantiable.";
  throw new BindingResolutionContractException($message);
 }
 $this->buildStack[] = $concrete;
 $constructor = $reflector->getConstructor();
 // If there are no constructors, that means there are no dependencies then
 // we can just resolve the instances of the objects right away, without
 // resolving any other types or dependencies out of these containers.
 if (is_null($constructor)) {
  array_pop($this->buildStack);
  return new $concrete;
 }
 $dependencies = $constructor->getParameters();
 // Once we have all the constructor''s parameters we can create each of the
 // dependency instances and then use the reflection instances to make a
 // new instance of this class, injecting the created dependencies in.
 $parameters = $this->keyParametersByArgument(
  $dependencies, $parameters
 );
 $instances = $this->getDependencies(
  $dependencies, $parameters
 );
 array_pop($this->buildStack);
 return $reflector->newInstanceArgs($instances);
}

登录后复制

具体从容器中获取实例的方法:

protected function resolveClass(ReflectionParameter $parameter)
{
 try {
  return $this->make($parameter->getClass()->name);
 }
 // If we can not resolve the class instance, we will check to see if the value
 // is optional, and if it is we will return the optional parameter value as
 // the value of the dependency, similarly to how we do this with scalars.
 catch (BindingResolutionContractException $e) {
  if ($parameter->isOptional()) {
   return $parameter->getDefaultValue();
  }
  throw $e;
 }
}

登录后复制

框架底层通过Reflection反射为开发节省了很多细节,实现了自动依赖注入。这里不做继续深入研究了。

写了一个模拟这个过程的类测试:

<&#63;php
class kulou
{
 //
}
class junjun
{
 //
}
class tanteng
{
 private $kulou;
 private $junjun;
 public function __construct(kulou $kulou,junjun $junjun)
 {
  $this->kulou = $kulou;
  $this->junjun = $junjun;
 }
}
//$tanteng = new tanteng(new kulou(),new junjun());
$reflector = new ReflectionClass(''tanteng'');
$constructor = $reflector->getConstructor();
$dependencies = $constructor->getParameters();
print_r($dependencies);exit;

登录后复制

原理是通过ReflectionClass类解析类的构造函数,并且取出构造函数的参数,从而判断依赖关系,从容器中取,并自动注入。

转自:小谈博客 http://www.tantengvip.com/2016/01/laravel-construct-ioc/

更多关于Laravel相关内容感兴趣的读者可查看本站专题:《Laravel框架入门与进阶教程》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于Laravel框架的PHP程序设计有所帮助。

我们今天的关于php基于str_pad实现卡号不足位数自动补0的方法的分享就到这里,谢谢您的阅读,如果想了解更多关于C# String 前面不足位数补零的方法、Java Android 开发数字不足位数前面补0、JS中的PadLeft、PadRight,位数不足,自动补位,String扩展方法、Laravel实现构造函数自动依赖注入的方法_PHP的相关信息,可以在本站进行搜索。

本文标签:

上一篇php中simplexml_load_file函数用法实例(php的implode)

下一篇PHP编程:PHP使用json_encode函数时不转义中文的解决方法(php json_decode中文乱码)