我尝试了一些很长的方法,但我认为我做错了什么。
这是我的代码
<?php print strtolower($blob); ?>
这使得$blob
小写,但另外我需要$blob
删除任何空格并用破折号(-
)替换。
我试过这个,但它没有用
<?php print (str_replace(' ', '-', $string)strtolower($blob)); ?>
我可以在一行中完成这一切吗?
我尝试了一些很长的方法,但我认为我做错了什么。
这是我的代码
<?php print strtolower($blob); ?>
这使得$blob
小写,但另外我需要$blob
删除任何空格并用破折号(-
)替换。
我试过这个,但它没有用
<?php print (str_replace(' ', '-', $string)strtolower($blob)); ?>
我可以在一行中完成这一切吗?
是的,只需将返回值strtolower($blob)
作为str_replace
(您拥有的$string
)的第三个参数传递。
<?php print (str_replace(' ', '-', strtolower($blob))); ?>
对于字符串换行,您可以使用专用的wordwrap功能。
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
自动换行
$str = 'Convert spaces to dash and LowerCase with PHP'; echo wordwrap(strtolower($str), 1, '-', 0); // return: convert-spaces-to-dash-and-lowercase-with-php
顺便说一句,在 WordPress 中,您可以使用sanitize_title_with_dashes
https://developer.wordpress.org/reference/functions/sanitize_title_with_dashes/