使用 PHP 将空格转换为破折号和小写

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28977793/
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-26 00:11:14  来源:igfitidea点击:

Convert spaces to dash and lowercase with PHP

phpstringreplaceconverters

提问by caustic

I've tried a few long methods but I think I'm doing something wrong.

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

Here is my code

这是我的代码

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

Which makes $bloblowercase, but additionally I need any spaces in $blobto be removed and replaced by a dash (-).

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

I tried this, but it didn't work

我试过这个,但没有用

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

Can I accomplish this all in the one line?

我可以在一条线上完成这一切吗?

回答by Alexander O'Mara

Yes, simply pass the return value of strtolower($blob)as the third argument of str_replace(where you have $string).

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

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

回答by Nolwennig

For a string wrap you can use the dedicated wordwrapfunction.

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

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

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

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

自动换行

自动换行在线文档

$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

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