31

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

这是我的代码

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

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

我试过这个,但它没有用

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

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

4

3 回答 3

73

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

<?php print (str_replace(' ', '-', strtolower($blob))); ?>
于 2015-03-11T02:57:57.347 回答
5

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

str_replace

str_replace 在线文档

<?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 在线文档

$str = 'Convert spaces to dash and LowerCase with PHP';

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

在线代码:https://3v4l.org/keWGr

于 2016-08-03T10:09:35.933 回答
2

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

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

于 2021-05-08T19:51:02.167 回答