Tuesday, 23 January 2018

Prefix and suffix

Prefixes or suffixes a string with n times char

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'<br/>str_suffix('test', 3, '-') => returns 'test---'<br/>

0 comments:

Post a Comment