运行 3 str_replace 更有效率
Run 3 str_replace more efficiently
所以我目前运行下面的代码:
$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);
不过我觉得这段代码可以更有效率。因为我在同一个字符串上 运行ning 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
的一部分
如何使这段代码最有效。
干杯
您可以使用数组通过 str_replace
方法查找和替换参数:
$current_link = str_replace(array(" ",".-","author"), array("-","-","authors"), $current_link);
所以我目前运行下面的代码:
$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);
不过我觉得这段代码可以更有效率。因为我在同一个字符串上 运行ning 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
如何使这段代码最有效。
干杯
您可以使用数组通过 str_replace
方法查找和替换参数:
$current_link = str_replace(array(" ",".-","author"), array("-","-","authors"), $current_link);