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; 
}
Snippet Details



str_prefix('test', 3, '-') => returns '---test'<br/>str_suffix('test', 3, '-') => returns 'test---'<br/>

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

bobchin February 12, 2008 at 02:54
function str_prefix($str, $n=1, $char=" "){
return str_repeat($char, $n) . $str;
}