Crop leading white space using PHP?

There seems to be an error in the Wordpress PHP function that leaves a space in front of the page title generated <?php echo wp_title(''); ?>. I went through Wordpress docs and forums on this feature with no luck.

I use it in such a way <body id="<?php echo wp_title(''); ?>">as to generate an HTML body tag with a page header id.

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

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

So how would I separate the space? Thanks Mark


Part Two of the Epic

John, a hexagonal dump was a good idea; it shows the space as two "20" spaces. But all the decisions shared by leading spaces and spaces have not been made.

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 header headers that look like chevrons that should be dropped when the parameter is false, and they are there, but the empty space is reset.

Is there a nuclear option?


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

+5
source share
6 answers
  • Clear all spaces from the left end of the header:

    <?php echo ltrim(wp_title('')); ?>
    
  • Remove all spaces from either end:

    <?php echo trim(wp_title('')); ?>
    
  • Remove all spaces from the left end of the header:

    <?php echo ltrim(wp_title(''), ' '); ?>
    
  • , :

    <?php echo str_replace(' ', '', wp_title(''), 1); ?>
    
  • ( , ) :

    <?php echo preg_replace('/^ /', '', wp_title('')); ?>
    
  • , :

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

Update

Wordpress wp_title , wp_title , false , . :

<?php echo trim(wp_title('', false)); ?>
+36
+10
ltrim($str)
+1

: trim

 <body id="<?=trim(wp_title('', false));?>">
+1

! , CSS , .

As a result, I got an additional obstacle, as some pages have headers with built-in spaces, so I finished this encoding:

<?php echo str_replace(' ','-',trim(wp_title('',false))); ?>
+1
source

add this to your functions. php

add_filter('wp_title', create_function('$a, $b','return str_replace(" $b ","",$a);'), 10, 2);

should work like a charm

0
source

All Articles