用 PHP 修剪前导空白?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1101322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:05:49  来源:igfitidea点击:

Trim leading white space with PHP?

phpwhitespacetrim

提问by markratledge

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.

Wordpress PHP 函数中似乎存在一个错误,它在生成的页面标题前留下空白,<?php echo wp_title(''); ?>我已经浏览了有关该函数的 Wordpress 文档和论坛,但没有任何运气。

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.

我以这种方式使用它<body id="<?php echo wp_title(''); ?>">是为了生成一个带有页面标题 id 的 HTML body 标记。

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">

所以我需要做的是去掉那个空白区域,让 body 标签看起来像这样<body id="mypage">而不是这样<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.

多余的空白会杀死我试图用来突出显示活动页面菜单项的 CSS。当我手动添加没有空格的正确 body 标记时,我的 CSS 可以工作。

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.

约翰,十六进制转储是个好主意;它将空白显示为两个“20”空格。但是所有去除前导空格和空白的解决方案都没有。

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

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

gives me < body id ="">

给我 < body id ="">

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

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

gives me < body id =" mypage">

给我 < 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.

谜。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

是的,之前都试过;他们仍然返回两个前导空格... arrgg

回答by John Kugelman

  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); ?>
    
  1. 去除标题左端的所有空格:

    <?php echo ltrim(wp_title('')); ?>
    
  2. 从任一端去除所有空格:

    <?php echo trim(wp_title('')); ?>
    
  3. 去掉标题左端的所有空格:

    <?php echo ltrim(wp_title(''), ' '); ?>
    
  4. 删除第一个空格,即使它不是第一个字符:

    <?php echo str_replace(' ', '', wp_title(''), 1); ?>
    
  5. 在开头只去掉一个空格(不是换行符,不是制表符):

    <?php echo preg_replace('/^ /', '', wp_title('')); ?>
    
  6. 去掉第一个字符,不管它是什么:

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

Update

更新

From the Wordpress documentation on wp_title, it appears that wp_titledisplays the title itself unless you pass falsefor the second parameter, in which case it returns it. So try:

从 上的 Wordpress文档中wp_titlewp_title除非您传递false第二个参数,否则它似乎会显示标题本身,在这种情况下它会返回它。所以尝试:

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

回答by Rob

回答by mpen

ltrim($str)

回答by Chris Thompson

Just to throw in some variety here: trim

只是在这里抛出一些变化:修剪

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

回答by Tomdkat

Thanks for this info! I was in the same boat in that I needed to generate page ids for CSS purposes based on the page title and the above solution worked beautifully.

感谢您提供此信息!我在同一条船上,因为我需要根据页面标题为 CSS 目的生成页面 id,并且上述解决方案工作得很好。

I ended up having an additional hurdle in that some pages have titles with embedded spaces, so I ended up coding this:

我最终遇到了一个额外的障碍,因为有些页面的标题带有嵌入的空格,因此我最终编写了以下代码:

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

回答by james

add this to your functions.php

将此添加到您的functions.php

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

should work like a charm

应该像魅力一样工作