Trim leading white space with PHP?

  1. Strip all whitespace from the left end of the title:

    <?php echo ltrim(wp_title('')); ?>
    
  2. Strip all whitespace from either end:

    <?php echo trim(wp_title('')); ?>
    
  3. Strip all spaces from the left end of the title:

    <?php echo ltrim(wp_title(''), ' '); ?>
    
  4. Remove the first space, even if it’s not the first character:

    <?php echo str_replace(' ', '', wp_title(''), 1); ?>
    
  5. Strip only a single space (not newline, not tab) at the beginning:

    <?php echo preg_replace('/^ /', '', wp_title('')); ?>
    
  6. Strip the first character, whatever it is:

    <?php echo substr(wp_title(''), 1); ?>
    

Update

From the WordPress documentation on wp_title, it appears that wp_title displays the title itself unless you pass false for the second parameter, in which case it returns it. So try:

<?php echo trim(wp_title('', false)); ?>

Leave a Comment