【发布时间】:2016-01-11 16:03:18
【问题描述】:
我有一个 PHP 函数应该执行以下任务:
该函数将采用 2 个参数 - 字符串和胶水(默认为“-”)。
对于给定的字符串,
-- 删除所有特殊字符
-- 小写
-- 删除多个空格
-- 用胶水 (-) 替换空格。
该函数以 $input 作为参数。我使用的代码如下:
//make all the charecters in lowercase
$low = strtolower($input);
//remove special charecters and multiple spaces
$nospecial = preg_replace('/[^a-zA-Z0-9\s+]/', '', $low);
//replace the spaces into glues (-). here is the problem.
$converted = preg_replace('/\s/', '-', $nospecial);
return $converted;
我没有发现这段代码有什么问题。但是在输出中显示了多个胶水。但我已经在代码的第二行中删除了多个空格。那么为什么它显示多个胶水?谁能有任何解决方案?
【问题讨论】:
-
对不起...它不起作用.. :(
标签: php regex preg-replace whitespace