简体   繁体   English

替换非字母数字字符的所有组合

[英]replace all combinations of non-alphanumeric characters

I need to to create url from various strings. 我需要从各种字符串创建url。
So trying to replace all non-alphanumeric characters and all of their combinations - with a hyphen character ( - ) 因此,尝试替换所有非字母数字字符and all of their combinations - 使用连字符( -

$string = "blue - sky";
$string = preg_replace("/[^A-Za-z0-9 ]/", '-', $string);
echo $string;  

result - blue---sky 结果 - blue---sky
expected - blue-sky . 预计 - blue-sky

使用+符号用一个替换字符替换多个字符:

string = preg_replace("/[^A-Za-z0-9]+/", '-', $string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM