can you help me in my problem.
你能解决我的问题吗?
i want to create a function to replace the space into dash then i found a solution for that
我想创建一个函数来将空格替换为破折号然后我找到了解决方案
this is the sulotion i found:
这是我发现的闷热:
function slug($phrase, $maxLength=100000000000000)
{
$result = strtolower($phrase);
$result = preg_replace("/[^A-Za-z0-9\s-._\/]/", "", $result);
$result = trim(preg_replace("/[\s-]+/", " ", $result));
$result = trim(substr($result, 0, $maxLength));
$result = preg_replace("/\s/", "-", $result);
return $result;
}
my problem is i want to create a vice versa for that replace dash to space
我的问题是我想创建一个反之亦然的替换短划线到空间
example input:
示例输入:
dash-to-space
DASH-到空间
example output
示例输出
dash to space
冲向太空
how can i do that?
我怎样才能做到这一点?
4 个解决方案
#1
2
You can use this lookaround based preg_replace
:
您可以使用此基于环视的preg_replace:
$result = preg_replace('/(?<!\s)-(?!\s)/', ' ', $input);
RegEx Demo
This will avoid replacing -
by space if it is surrounded by space.
如果被空间包围,这将避免替换空间。
#2
2
the first lines are only cleaning up the string.
第一行只是清理字符串。
The last line performs the replace function.
最后一行执行替换功能。
switching the parameters around should do the trick
切换参数应该可以解决问题
$result = preg_replace("/\-/", " ", $result);
$ result = preg_replace(“/ \ - /”,“”,$ result);
#3
1
It will help you :
它会帮助你:
$output = str_replace(',', ' ', $result);
#4
1
$result = str_replace('-', ' ', $result);
=> - to spaces
$ result = str_replace(' - ','',$ result); => - 到空格
$result = str_replace(' ', '-', $result);
=> spaces to -
$ result = str_replace('',' - ',$ result); =>空格到 -
#1
2
You can use this lookaround based preg_replace
:
您可以使用此基于环视的preg_replace:
$result = preg_replace('/(?<!\s)-(?!\s)/', ' ', $input);
RegEx Demo
This will avoid replacing -
by space if it is surrounded by space.
如果被空间包围,这将避免替换空间。
#2
2
the first lines are only cleaning up the string.
第一行只是清理字符串。
The last line performs the replace function.
最后一行执行替换功能。
switching the parameters around should do the trick
切换参数应该可以解决问题
$result = preg_replace("/\-/", " ", $result);
$ result = preg_replace(“/ \ - /”,“”,$ result);
#3
1
It will help you :
它会帮助你:
$output = str_replace(',', ' ', $result);
#4
1
$result = str_replace('-', ' ', $result);
=> - to spaces
$ result = str_replace(' - ','',$ result); => - 到空格
$result = str_replace(' ', '-', $result);
=> spaces to -
$ result = str_replace('',' - ',$ result); =>空格到 -