【问题标题】:Fastest way to match uri string匹配uri字符串的最快方法
【发布时间】:2026-01-14 08:20:02
【问题描述】:

以下代码在我网站的index.php 文件中,每次查询我网站上的页面时都会运行。这两个requires 都在那里执行各自的404 错误页面,所以不用担心。在 php 中执行此操作的最高效的方法是什么?

$cache = $_SERVER['REQUEST_URI'];

if(preg_match('/^\/blog/',$cache) || preg_match('/^\/portfolio/',$cache)){
  define('WP_USE_THEMES', true);
  // Loads the WordPress Environment and Template
  require('./wordpress/wp-blog-header.php'); 
}else{
  // Load codeigniter
  require('./codeigniter/index.php');
}

【问题讨论】:

    标签: php regex indexing uri


    【解决方案1】:
    $cache = $_SERVER['REQUEST_URI'];
    
    if (0 === strpos($cache, '/blog/') ||
        0 === strpos($cache, '/portfolio/'))
    {
        define('WP_USE_THEMES', true);
        require('./wordpress/wp-blog-header.php'); 
    }
        else
    {
        require('./codeigniter/index.php');
    }
    

    不需要正则表达式,而且速度超级快。

    【讨论】:

    • 结束斜线是为了不匹配以博客或投资组合开头的不同 uri。我怀疑这种情况会发生,尽管哈哈感谢您选择我的答案,而其他人都没有。
    最近更新 更多