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

cau*_*tic 22 php string replace converters

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

这是我的代码

<?php print strtolower($blob); ?>
Run Code Online (Sandbox Code Playgroud)

这使得$blob小写,但另外我需要$blob删除任何空格并用短划线(-)替换.

我尝试了这个,但它没有用

<?php print (str_replace(' ', '-', $string)strtolower($blob)); ?>
Run Code Online (Sandbox Code Playgroud)

我能在一行中完成这一切吗?

Ale*_*ara 51

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

<?php print (str_replace(' ', '-', strtolower($blob))); ?>
Run Code Online (Sandbox Code Playgroud)


Nol*_*nig 5

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

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
Run Code Online (Sandbox Code Playgroud)

换行

自动换行在线文档

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

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

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