Home  >  Article  >  Backend Development  >  php怎么截取字符串并以零补齐str_pad() 函数

php怎么截取字符串并以零补齐str_pad() 函数

WBOY
WBOYOriginal
2016-06-13 11:00:03913browse

php如何截取字符串并以零补齐str_pad() 函数

定义和用法

str_pad() 函数把字符串填充为指定的长度。

语法

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

可选。规定填充字符串的那边。

可能的值:

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











例子 1

$str = "http://www.my400800.cn";
echo str_pad($str,30,".");
?>

输出:

http://www.my400800.cn........

例子 2

$str = "http://www.my400800.cn";
echo str_pad($str,30,".",STR_PAD_LEFT);
?>

输出:

........http://www.my400800.cn

例子 3

$str = "http://www.my400800.cn";
echo str_pad($str,30,".:",STR_PAD_BOTH);
?>

输出:

.:.:http://www.my400800.cn.:.:
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn