str_pad

  • (PHP 4 >= 4.0.1, PHP 5, PHP 7)
  • str_pad — Pad a string to a certain length with another string
  • str_pad — 使用另一个字符串填充字符串为指定长度

Description

string str_pad (
string $input ,
int $pad_length [,
string $pad_string = " " [,
int $pad_type = STR_PAD_RIGHT ]]
)
//This function returns the input string padded on the left, the right, or both sides to the specified padding length. If the optional argument pad_string is not supplied, the input is padded with spaces, otherwise it is padded with characters from pad_string up to the limit.
//该函数返回 input 被从左端、右端或者同时两端被填充到制定长度后的结果。如果可选的 pad_string 参数没有被指定,input 将被空格字符填充,否则它将被 pad_string 填充到指定长度。

Parameters

input

  • The input string.
  • 输入字符串。

pad_length

  • If the value of pad_length is negative, less than, or equal to the length of the input string, no padding takes place, and input will be returned.
  • 如果 pad_length 的值是负数,小于或者等于输入字符串的长度,不会发生任何填充,并会返回 input 。

pad_string

Note:

  • The pad_string may be truncated if the required number of padding characters can't be evenly divided by the pad_string's length.
  • 如果填充字符的长度不能被 pad_string 整除,那么 pad_string 可能会被缩短。

pad_type

  • Optional argument pad_type can be STR_PAD_RIGHT, STR_PAD_LEFT, or STR_PAD_BOTH. If pad_type is not specified it is assumed to be STR_PAD_RIGHT.
  • 可选的 pad_type 参数的可能值为 STR_PAD_RIGHTSTR_PAD_LEFTSTR_PAD_BOTH。如果没有指定 pad_type,则假定它是 STR_PAD_RIGHT

Return Values

  • Returns the padded string.
  • 返回填充后的字符串。

Examples

<?php
/**
* Created by PhpStorm.
* User: zhangrongxiang
* Date: 2018/2/19
* Time: 下午10:59
*/ //function str_pad ($input, $pad_length, $pad_string = null, $pad_type = null) {} $input = "zing";
$str = str_pad( $input, 10 );
//"zing "
echo $str . PHP_EOL;
//10
echo strlen( $str ) . PHP_EOL; //zing******
echo str_pad( $input, 10, "*" ) . PHP_EOL; //^^^^^^zing
echo str_pad( $input, 10, "^", STR_PAD_LEFT ) . PHP_EOL; // 输出 "-=-=-Alien" //___zing___
echo str_pad( $input, 10, "_", STR_PAD_BOTH ) . PHP_EOL; //zing=
echo str_pad( $input, 5, "==" ) . PHP_EOL; //zing
echo str_pad( $input, 3, "-" ) . PHP_EOL; //////////////////////////////////////////////////////////////
/**
* since the default pad_type is STR_PAD_RIGHT.
* using STR_PAD_BOTH were always favor in the right pad
* if the required number of padding characters can't be evenly divided.
*/ //ppinputppp
echo str_pad( "input", 10, "pp", STR_PAD_BOTH ) . PHP_EOL;
//inputp
echo str_pad( "input", 6, "p", STR_PAD_BOTH ) . PHP_EOL;
//pinputpp
echo str_pad( "input", 8, "p", STR_PAD_BOTH ) . PHP_EOL; /////////////////////////////////////////////////////////////
function str_pad_unicode( $str, $pad_len, $pad_str = ' ', $dir = STR_PAD_RIGHT ) {
$str_len = mb_strlen( $str );
$pad_str_len = mb_strlen( $pad_str );
if ( ! $str_len && ( $dir == STR_PAD_RIGHT || $dir == STR_PAD_LEFT ) ) {
$str_len = 1; // @debug
}
if ( ! $pad_len || ! $pad_str_len || $pad_len <= $str_len ) {
return $str;
} $result = null;
$repeat = ceil( $str_len - $pad_str_len + $pad_len );
if ( $dir == STR_PAD_RIGHT ) {
$result = $str . str_repeat( $pad_str, $repeat );
$result = mb_substr( $result, 0, $pad_len );
} else if ( $dir == STR_PAD_LEFT ) {
$result = str_repeat( $pad_str, $repeat ) . $str;
$result = mb_substr( $result, - $pad_len );
} else if ( $dir == STR_PAD_BOTH ) {
$length = ( $pad_len - $str_len ) / 2;
$repeat = ceil( $length / $pad_str_len );
$result = mb_substr( str_repeat( $pad_str, $repeat ), 0, floor( $length ) )
. $str
. mb_substr( str_repeat( $pad_str, $repeat ), 0, ceil( $length ) );
} return $result;
} $str = "拍黄片";
//哈拍黄片哈
echo str_pad_unicode( $str, 5, '哈', STR_PAD_BOTH ) . PHP_EOL;
//哈哈哈拍黄片哈哈哈哈
echo str_pad_unicode( $str, 10, '哈', STR_PAD_BOTH ) . PHP_EOL; function mb_str_pad( $str, $pad_len, $pad_str = ' ', $dir = STR_PAD_RIGHT, $encoding = null ) {
$encoding = $encoding === null ? mb_internal_encoding() : $encoding;
$padBefore = $dir === STR_PAD_BOTH || $dir === STR_PAD_LEFT;
$padAfter = $dir === STR_PAD_BOTH || $dir === STR_PAD_RIGHT;
$pad_len -= mb_strlen( $str, $encoding );
$targetLen = $padBefore && $padAfter ? $pad_len / 2 : $pad_len;
$strToRepeatLen = mb_strlen( $pad_str, $encoding );
$repeatTimes = ceil( $targetLen / $strToRepeatLen );
$repeatedString = str_repeat( $pad_str, max( 0, $repeatTimes ) ); // safe if used with valid utf-8 strings
$before = $padBefore ? mb_substr( $repeatedString, 0, floor( $targetLen ), $encoding ) : '';
$after = $padAfter ? mb_substr( $repeatedString, 0, ceil( $targetLen ), $encoding ) : ''; return $before . $str . $after;
} //哈拍黄片哈
echo mb_str_pad( $str, 5, '哈', STR_PAD_BOTH ) . PHP_EOL;
//哈哈哈拍黄片哈哈哈哈
echo mb_str_pad( $str, 10, '哈', STR_PAD_BOTH ) . PHP_EOL; ///////////////////////////////////////////////////////////////////////
function pad_between_strings( $string1, $string2, $length, $char = " " ) {
$fill_length = $length - ( strlen( $string1 ) + strlen( $string2 ) ); return $string1 . str_repeat( $char, $fill_length ) . $string2;
} //abc**************123
echo pad_between_strings( "abc", "123", 20, "*" ) . PHP_EOL;

See

All rights reserved

PHP之string之str_pad()函数使用的更多相关文章

  1. php str_pad()函数 语法

    php str_pad()函数 语法 str_pad()函数怎么用? php str_pad()函数用于把字符串填充到指定长度,语法是str_pad(string,length,pad_string, ...

  2. php的str_pad()函数

    实例 填充字符串的右侧,到30个字符的新长度 <?php $str = "Hello World"; echo str_pad($str,30,"."); ...

  3. OC与c混编实现Java的String的hashcode()函数

    首先,我不愿意大家需要用到这篇文章里的代码,因为基本上你就是被坑了. 起因:我被Java后台人员坑了一把,他们要对请求的参数增加一个额外的字段,字段的用途是来校验其余的参数是否再传递过程中被篡改或因为 ...

  4. string类find函数返回值判定

     string类find函数返回值判定 代码示例 #include<iostream> #include<cstring> using namespace std; int m ...

  5. C string.h 常用函数

    参考:http://womendu.iteye.com/blog/1218155 http://blog.csdn.net/zccst/article/details/4294565 还有一些,忘记了 ...

  6. c++中string的常用函数说明

    string可以说是是字符数组的升级版,使用更加啊方便,不容易出错.本文对string的常用函数进行简单介绍,做到会用即可. string中的常用函数分为四类,即赋值,添加,比较和删除. 一.赋值 1 ...

  7. C++ string类及其函数的讲解

    文章来源于:http://www.cnblogs.com/hailexuexi/archive/2012/02/01/2334183.html C++中string是标准库中一种容器,相当于保存元素类 ...

  8. PHP之string之explode()函数使用

    explode (PHP 4, PHP 5, PHP 7) explode - Split a string by string explode - 使用一个字符串分割另一个字符串 Descripti ...

  9. PHP str_pad() 函数

    str_pad() 函数把字符串填充为指定的长度. 进入 详细介绍页面

随机推荐

  1. [翻译] Writing Property Editors 编写属性编辑器

    Writing Property Editors 编写属性编辑器   When you select a component in the designer its properties are di ...

  2. JDBC 中 socketTimeout 的作用

    如果我们把socketTimeout设置如下: socketTimeout=60000; 这意味着60秒以内服务器必须开始给客户端吐数据,以保持socket的活性.配置成60秒,一般查询都不会遇到问题 ...

  3. solr特点八:Spatial(空间搜索)

    前言 在美团CRM系统中,搜索商家的效率与公司的销售额息息相关,为了让BD们更便捷又直观地去搜索商家,美团CRM技术团队基于Solr提供了空间搜索功能,其中移动端周边商家搜索和PC端的地图模式搜索功能 ...

  4. spark任务提交到yarn上命令总结

    spark任务提交到yarn上命令总结 1. 使用spark-submit提交任务 集群模式执行 SparkPi 任务,指定资源使用,指定eventLog目录 spark-submit --class ...

  5. pycharm中安装可以贴图片的Markdown插件

    方法一:(测试成功) 先安装官方推荐的Markdown support插件,再安装Paste images into MarkDown 如果Paste images into MarkDown插件在线 ...

  6. C# 使用 COALESCE 表达式简化 Null 检查

    下面这个判断 null 的语句: a == null ? b: a 可以用 COALESCE 表达式,简化成: a ?? b

  7. C#使用LitJson对Json数据解析

    JSON 介绍 JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于JavaScript(Standard ECMA-262 3rd Edition - ...

  8. roadflow asp.net工作流自定义表单

    在roadflow表单设计器不能满足很复杂的业务需求的时候,可以采用自定义表单(即表单页面自己做). 自定义表单就是自己写一个页面,包含控制器视图,然后将这个页面挂到流程上进行审批. 自定义表单分为以 ...

  9. [uwp]MVVM模式实战之必应壁纸查看器

    最近学习MVVM,至于什么是MVVM我也在这儿不多说了,一是关于它的解释解释网上非常多,二是我怕自己讲不清,误导自己没关系,误导别人就不好了.. 好了,废话结束,看是实战...... 这个必应壁纸的d ...

  10. SQL语句优化 (一) (52)

    优化SQL语句的一般步骤 1 通过show status命令了解各种SQL的执行频率. 格式:mysql> show [session|global]status; 其中:session(默认) ...