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. 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|"; ? 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() 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
Archived
This topic is now archived and is closed to further replies.