2017-06-14 87 views
0

所以我目前運行下面的代碼:運行3 str_replace函數更有效地

$current_link = get_author_posts_url($user_id,strtolower($user_info->user_login)); 
$current_link = str_replace(" ", "-", $current_link); 
$current_link = str_replace(".-", "-", $current_link); 
$current_link = str_replace("author", "authors", $current_link); 

但是我覺得這個代碼可能是更有效的。因爲我在同一個字符串上運行str_replace 3次。 所以我用preg_replace最小化,像這樣的代碼:

$cLPatterns = array(' ', '.-'); 
$current_link = preg_replace($cLPatterns, '-', $current_link); 
$current_link = str_replace("author", "authors", $current_link); 

但有使用str_replace("author", "authors", $current_link)preg_replace

一部分的方式我怎樣才能讓這段代碼最有效的。

乾杯

+1

str_replace函數需要數組作爲參數 – rtfm

回答

1

您可以使用陣列來查找和與str_replace方法替換參數:

$current_link = str_replace(array(" ",".-","author"), array("-","-","authors"), $current_link); 
+0

啊OK呀,這就是我要找的,感謝@ RTFM –