Omzy Posted March 4, 2010 Share Posted March 4, 2010 I want to pad a string with the | (pipe) character at the beginning and end of the string. I tried str_pad() but that won't work. I can obviously add the character in manually but was just wondering if there was a function out there that could do it. Quote Link to comment https://forums.phpfreaks.com/topic/194148-pad-the-string-with-a-character/ Share on other sites More sharing options...
salathe Posted March 4, 2010 Share Posted March 4, 2010 Define "pad", if you want to just add that character then why not $string = "|$string|"; ? Quote Link to comment https://forums.phpfreaks.com/topic/194148-pad-the-string-with-a-character/#findComment-1021507 Share on other sites More sharing options...
Psycho Posted March 4, 2010 Share Posted March 4, 2010 Any, why won't string_pad() work? It works fine for me: <?php $text = "My Text"; echo str_pad($text, 10, "|"); //Output: My Text||| echo str_pad($text, 10, "|", STR_PAD_LEFT); //Output: |||My Text echo str_pad($text, 10, "|", STR_PAD_BOTH); //Output: |My Text|| ?> Of course, you can also use printf()/sprintf() Quote Link to comment https://forums.phpfreaks.com/topic/194148-pad-the-string-with-a-character/#findComment-1021532 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.