php为字符串前后添加指定数量字符
php自定义函数:str_prefix与str_suffix,为字符串前后添加指定数量的符号。
函数代码:
复制代码 代码示例:
function str_prefix($str, $n=1, $char=" "){
for ($x=0;$x<$n;$x++){$str = $char.$str;}
return $str;
}
function str_suffix($str, $n=1, $char=" "){
for ($x=0;$x<$n;$x++){$str = $str.$char;}
return $str;
}
例子:
str_prefix('test',3,'-')=>returns'---test'
str_suffix('test',3,'-')=> returns 'test---'