如何使用preg_replace更改此ereg_replace?

时间:2012-11-01 15:02:15

标签: php preg-replace ereg-replace

  

可能重复:
  How can I convert ereg expressions to preg in PHP?

我有能力制作我在网上找到的SEO链接的功能,但是使用最新的PHP它因为ereg_replace而无效。 有人可以帮我把它改成preg_replace。

function generate_seo_link($FieldName, &$CurrVal, &$CurrPrm){
        //make it lowercase, remove punctuation, remove multiple/leading/ending spaces
        $rep = trim(ereg_replace(' +',' ',preg_replace('/[^a-zA-Z0-9\s]/','',strtolower($CurrVal))));
        //convert the spaces to whatever the user wants
        //usually a dash or underscore..
        //...then return the value.
        $CurrVal = str_replace(' ','-',$rep);
}

2 个答案:

答案 0 :(得分:3)

您只需将ereg_replace更改为preg_replace,就像这样

function generate_seo_link($FieldName, &$CurrVal, &$CurrPrm){
    //make it lowercase, remove punctuation, remove multiple/leading/ending spaces
    $rep = trim(preg_replace('/ +/',' ',preg_replace('/[^a-zA-Z0-9\s]/','',strtolower($CurrVal))));
    //convert the spaces to whatever the user wants
    //usually a dash or underscore..
    //...then return the value.
    $CurrVal = str_replace(' ','-',$rep);
}

对于preg_replace,您必须使用分隔符封装正则表达式(注意正则表达式中的周围/)。

答案 1 :(得分:1)

让我们看看。将ereg_replace更改为preg_replace。你不需要帮助,只需改变它。