Heim  >  Artikel  >  Backend-Entwicklung  >  php怎么截取字符串并以零补齐str_pad() 函数

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

WBOY
WBOYOriginal
2016-06-13 13:04:071229Durchsuche

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.:.:
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn