用PHP修剪领先的空格? [英] Trim leading white space with PHP?

查看:78
本文介绍了用PHP修剪领先的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Wordpress PHP函数中似乎存在一个错误,使得空格留在了<?php echo wp_title(''); ?>生成的页面标题的前面.我在该函数上浏览过Wordpress文档和论坛,没有任何运气.

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.

我正以这种方式使用它<body id="<?php echo wp_title(''); ?>">,以生成带有页面标题ID的HTML正文标签.

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标签看起来像这样的<body id="mypage">而不是这个<body id=" mypage">

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

多余的空格会杀死我要用来突出显示活动页面菜单项的CSS.当我手动添加正确的body标签而没有空格时,我的CSS起作用了.

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

史诗第二部分

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

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.

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

给我< body id ="">

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

给我< body id =" mypage">

拼图.问题的根源在于wp_title具有可选的页面标题前导字符-看起来像人字形-如果该选项为false则应该将其删除,但实际上是空白,但转储了空白.

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.

有核选择吗?

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

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

推荐答案

  1. 从标题的左端剥离所有空格:

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

<?php echo ltrim(wp_title('')); ?>

  • 从两端剥离所有空格:

  • Strip all whitespace from either end:

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

  • 在标题的左端去除所有空格:

  • Strip all spaces from the left end of the title:

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

  • 删除第一个空格,即使它不是第一个字符:

  • Remove the first space, even if it's not the first character:

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

  • 在开头仅带一个空格(不是换行符,不是制表符):

  • Strip only a single space (not newline, not tab) at the beginning:

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

  • 剥离第一个字符,不管它是什么:

  • Strip the first character, whatever it is:

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

  • 更新

    wp_title 上的Wordpress 文档中,看来wp_title显示了标题本身,除非您为第二个参数传递false,在这种情况下它将返回它.因此,尝试:

    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)); ?>
    

    这篇关于用PHP修剪领先的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆