【问题标题】:Convert spaces to dash and lowercase with PHP使用 PHP 将空格转换为破折号和小写
【发布时间】:2015-05-12 17:19:12
【问题描述】:

我尝试了一些很长的方法,但我认为我做错了什么。

这是我的代码

<?php print strtolower($blob); ?>

这使$blob 小写,但另外我需要删除$blob 中的任何空格并用破折号(-)替换。

我试过了,但是没用

<?php print (str_replace(' ', '-', $string)strtolower($blob)); ?>

我可以在一行中完成这一切吗?

【问题讨论】:

    标签: php string replace converters


    【解决方案1】:

    是的,只需将strtolower($blob) 的返回值作为str_replace 的第三个参数传递(您有$string)。

    <?php print (str_replace(' ', '-', strtolower($blob))); ?>
    

    【讨论】:

    • 如何将 Glossary A - Z 转换为 Glossary-a-z?
    【解决方案2】:

    对于字符串换行,您可以使用专用的wordwrap 函数。

    str_replace

    str_replace online documentation

    <?php
    
    $str = 'Convert spaces to dash and LowerCase with PHP';
    
    echo str_replace(' ', '-', strtolower($str));
    // return: convert-spaces-to-dash-and-lowercase-with-php
    

    自动换行

    wordwrap online documentation

    $str = 'Convert spaces to dash and LowerCase with PHP';
    
    echo wordwrap(strtolower($str), 1, '-', 0);
    // return: convert-spaces-to-dash-and-lowercase-with-php
    

    online code: https://3v4l.org/keWGr

    【讨论】:

      【解决方案3】:

      顺便说一句,在 WordPress 中你可以使用 sanitize_title_with_dashes

      https://developer.wordpress.org/reference/functions/sanitize_title_with_dashes/

      【讨论】:

      • 随机,但这正是我要找的 ty。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-09
      • 2013-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多