Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trim leading white space with PHP?

There seems to be a bug in a Wordpress PHP function that leaves whitespace in front of the title of the page generated by <?php echo wp_title(''); ?> I've been through the Wordpress docs and forums on that function without any luck.

I'm using it this way <body id="<?php echo wp_title(''); ?>"> in order to generate an HTML body tag with the id of the page title.

So what I need to do is strip that white space, so that the body tag looks like this <body id="mypage"> instead of this <body id=" mypage">

The extra white space kills the CSS I'm trying to use to highlight menu items of the active page. When I manually add a correct body tag without the white space, my CSS works.

So how would I strip the white space? Thanks, Mark


Part Two of the Epic

John, A hex dump was a good idea; it shows the white space as two "20" spaces. But all solutions that strip leading spaces and white space didn't.

And, <?php ob_start(); $title = wp_title(''); ob_end_clean(); echo $title; ?>

gives me < body id ="">

and <?php ob_start(); $title = wp_title(''); echo $title; ?>

gives me < body id =" mypage">

Puzzle. The root of the problem is that wp_title has optional page title leading characters - that look like chevrons - that are supposed to be dropped when the option is false, and they are, but white space gets dumped in.

Is there a nuclear option?


Yup, tried them both before; they still return two leading spaces... arrgg

like image 732
markratledge Avatar asked Jul 09 '09 01:07

markratledge


People also ask

How do I strip whitespace in PHP?

The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string.

How do you trim white spaces from a string?

String.prototype.trim() The trim() method removes whitespace from both ends of a string and returns a new string, without modifying the original string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).

What does PHP do with whitespace?

The ctype_space() function in PHP check for whitespace character(s). It returns TRUE if every character in text creates some sort of white space, FALSE otherwise. Besides the blank character this also includes tab, vertical tab, line feed, carriage return and form feed characters.


1 Answers

  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)); ?>
like image 137
13 revs, 2 users 96% Avatar answered Oct 21 '22 04:10

13 revs, 2 users 96%