gpt4 book ai didi

PHP大写转小写重写优化?

转载 作者:行者123 更新时间:2023-12-01 19:53:47 26 4
gpt4 key购买 nike

我使用此 PHP 代码将 URI 中任何形式的大写重定向为小写。有三个异常(exception):如果 URI 包含“adminpanel”或“search”,则不会重定向,如果它已经是小写,则不会重定向

您认为有什么方法可以改进 PHP 中的功能吗?

$trailed = $_SERVER['REQUEST_URI'];
$pos1 = strpos($trailed,"adminpanel");
$pos2 = strpos($trailed,"search");
if ($pos1 === false && $pos2 === false && strlen($trailed) !== strlen(preg_replace('/[A-Z]/', '', $trailed))) {
$trailed = strtolower($trailed);
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://'. $_SERVER["SERVER_NAME"] . $trailed);
exit;
}

最佳答案

我认为在 URI 大小写混合的情况下,这将无法重定向。这是故意的吗?另外,使用 $trailed 和 strtolower($trailed) 的字符串比较是否比在第 4 行 if 语句的第三个子句中使用正则表达式更简洁?

关于PHP大写转小写重写优化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1862329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com