所以我目前运行以下代码:
$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
如何才能使此代码最有效。
干杯
答案 0 :(得分:1)
您可以使用str_replace
方法将数组用于查找和替换参数:
$current_link = str_replace(array(" ",".-","author"), array("-","-","authors"), $current_link);