Php 我怎样才能替换“:&引用;加上/&引用;在slagify函数中?

Php 我怎样才能替换“:&引用;加上/&引用;在slagify函数中?,php,regex,Php,Regex,我有一个函数,可以对文本进行分段,除了我需要将“:”替换为“/”之外,它工作得很好。目前,它将所有非字母或数字替换为“-”。这是: function slugify($text) { // replace non letter or digits by - $text = preg_replace('~[^\\pL\d]+~u', '-', $text); // trim $text = trim($text, '-');

我有一个函数,可以对文本进行分段,除了我需要将“:”替换为“/”之外,它工作得很好。目前,它将所有非字母或数字替换为“-”。这是:

function slugify($text)
    {
        // replace non letter or digits by -
        $text = preg_replace('~[^\\pL\d]+~u', '-', $text);

        // trim
        $text = trim($text, '-');

        // transliterate
        if (function_exists('iconv'))
        {
            $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
        }

        // lowercase
        $text = strtolower($text);

        // remove unwanted characters
        $text = preg_replace('~[^-\w]+~', '', $text);

        if (empty($text))
        {
            return 'n-a';
        }

        return $text;
    }

我只做了几处修改。我提供了一组搜索/替换数组,让我们用
-
替换大部分内容,但用
/
替换

$search = array( '~[^\\pL\d:]+~u', '~:~' );
$replace = array( '-', '/' );
$text = preg_replace( $search, $replace, $text);
后来,最后一个
preg\u replace
将我们的
/
替换为一个空字符串。所以我允许在character类中使用前斜杠

$text = preg_replace('~[^-\w\/]+~', '', $text);
其输出如下:

// antiques/antiquities
echo slugify( "Antiques:Antiquities" );

是的,我知道,但是slagify函数呢?我的意思是,如果我只想替换“:”的话,我可以简单地做一个str_replace,但是我仍然需要对字符串进行段塞(删除特殊字符等),除了“:”应该替换为“/”而不是“-”。@mihai你把它放在段塞函数中,作为过程的一部分。不起作用。“:”仍替换为“-”。我认为这是有意义的,因为我们有$text=preg_replace(“~[^\\pL\d]+~u',“-”,$text);不是吗?@mihai您将什么字符串传递到Slagify?$text='antiquies:Antiquities';回音slagify($text);我买古董