2011-08-05 58 views
0

我有這個代碼片段從舊自從安裝標點符號ereg_replace的preg_replace

$pattern = $this->attributes['SEO_REMOVE_ALL_SPEC_CHARS'] == 'true' 
        ? "([^[:alnum:]])+" 
        : "([[:punct:]])+"; 

我想修改[:PUNCT:]選擇,因此排除了 - 衝刺。的代碼

下一行是

$anchor = ereg_replace($pattern, '', strtolower($string)); 

其去除先前發現的字符。我怎樣才能保持我的破折號?

感謝,馬里奧

編輯

我覺得我得到它:

$pattern = $this->attributes['SEO_REMOVE_ALL_SPEC_CHARS'] == 'true' 
        ? "([^[:alnum:]])+" 
        : "([^-a-zA-Z0-9[:space:]])+"; 

注:破折號必須是第一位的。或者,對於下劃線:

$pattern = $this->attributes['SEO_REMOVE_ALL_SPEC_CHARS'] == 'true' 
        ? "([^[:alnum:]])+" 
        : "([^a-zA-Z0-9_[:space:]])+"; 

我也沒弄明白怎麼用負向前看符號:( 乾杯馬里奧

回答

1

你可能需要使自己的[characterset]而不是使用[:punct:]

這個看起來正確的,但你需要驗證它。

[^a-zA-Z0-9-\s] 

這將取代任何爲n ot(a-z)字母,數字,空格或破折號。

$pattern = $this->attributes['SEO_REMOVE_ALL_SPEC_CHARS'] == 'true' 
      ? "([^[:alnum:]])+" 
      : "[^a-zA-Z0-9-\s]+"; 

編輯:老答案,那將無法​​正常工作因爲ereg doesn't support lookaround

試試這個負先行(?!-)

$pattern = $this->attributes['SEO_REMOVE_ALL_SPEC_CHARS'] == 'true' 
       ? "([^[:alnum:]])+" 
       : "((?!-)[[:punct:]])+";