Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么我的函数会在这个URL中添加一个额外的破折号?(PHP:Regex)_Php_Regex - Fatal编程技术网

为什么我的函数会在这个URL中添加一个额外的破折号?(PHP:Regex)

为什么我的函数会在这个URL中添加一个额外的破折号?(PHP:Regex),php,regex,Php,Regex,当我运行函数时,它会将“a-boy”变成“a-boy-”,但是我想要“a-boy”,如何修改正则表达式来解决这个问题 谢谢 看看这个,它很好用 function convert($raw) { $new = preg_replace("![^a-z0-9]+!i", "-", $raw); return $new; } function convert($raw) { $new = preg_replace("![^a-z0-9]+!i", "-", $raw)

当我运行函数时,它会将“a-boy”变成“a-boy-”,但是我想要“a-boy”,如何修改正则表达式来解决这个问题


谢谢

看看这个,它很好用

function convert($raw) {

    $new = preg_replace("![^a-z0-9]+!i", "-", $raw);

    return $new;

}
function convert($raw) {

    $new = preg_replace("![^a-z0-9]+!i", "-", $raw);
    $new = rtrim($new,'-');
    return $new;

}

您的预期输出是什么?您的输入有误。应该是
return$new而不是
返回$raw
function convert($raw) {

    $new = preg_replace("![^a-z0-9]+!i", " ", $raw);

    return $new;

}

$data ="a - boy";
echo convert($data);