2017-06-14 83 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 –