str_pad() - 使用另一个字符串填充字符串为指定长度 - php 字符串函数
str_pad()
(PHP 4 >= 4.0.1, PHP 5, PHP 7)
使用另一个字符串填充字符串为指定长度
说明
str_pad(string $input, int $pad_length[,string $pad_string= ""[,int $pad_type= STR_PAD_RIGHT]]) : string该函数返回$input被从左端、右端或者同时两端被填充到制定长度后的结果。如果可选的$pad_string参数没有被指定,$input将被空格字符填充,否则它将被$pad_string填充到指定长度。
参数
$input输入字符串。
$pad_length如果$pad_length的值是负数,小于或者等于输入字符串的长度,不会发生任何填充,并会返回$input。
$pad_stringNote:如果填充字符的长度不能被$pad_string整除,那么$pad_string可能会被缩短。$pad_type
可选的$pad_type参数的可能值为STR_PAD_RIGHT
,STR_PAD_LEFT
或STR_PAD_BOTH
。如果没有指定$pad_type,则假定它是STR_PAD_RIGHT
。
返回值
返回填充后的字符串。
范例
Example #1str_pad()范例
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. e.g
A proper unicode string padder; Test; Output; len 3: ... len 3: ... len 4: ...A len 4: ...Ä len 5: A...A len 5: Ä...Ä len 6: A...AO len 6: Ä...ÄÖ ...
multibyte version:
For me this worked. $string = 'help'; #First, str_pad() with unique character. $string = str_pad($string, 10, "*", STR_PAD_BOTH); #$string = '***help***'; #Second, str_replace with ' ' $string = str_replace("*", " ", $string);
a different, more robust multibyte version of str_pad that works correctly only if $pad_string is non-multibyte string function my_mb_str_pad($input, $pad_length, $pad_string=' ', $pad_type=STR_PAD_RIGHT,$encoding='UTF-8'){ $mb_diff=mb_strlen($str, $encoding)-strlen($string); return str_pad($input,$pad_length+$mb_diff,$pad_string,$pad_type); }
Warning: If your string includes non-ascii characters (eg the British pounds sign), str_pad() will treat these as two characters when calculating the padding. So for example: will produce a different length string depending on whether $currency_symbol is pounds or dollars. Hope this helps someone -- it caused me a lot of problems with misaligned columns in my invoices until I worked it out.
Fills the first argument (mostly a number, f.e. from a loop to display a date or time) with zeroes. sprintf() is indeed faster than str_pad.
In a lot of cases you're better off using str_repeat if you want to use something like - it repeats the entire string. Using str_repeat, I wrote a full string pad function that should closely mimic str_pad in every other way:
str_pad() can provide sufficient "zero padding" when using block ciphers and manual padding with openssl_encrypt() and similar. The example below will pad the 6 character text "Secret" with two \x00 characters and return 8 characters of data. Substitute your plain text and block size as needed.
鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com
免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)
图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!
内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)