2011-09-02 13 views
0

以下代碼位於我網站的index.php文件中,並且每次查詢我的網站上的某個頁面時都會運行該代碼。這兩個requires執行那裏各自的404錯誤頁面,所以不要擔心。什麼是最有效的方式做這個在PHP內?匹配uri字符串的最快方法

$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'); 
} 

回答

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'); 
} 

沒有正則表達式的需要,它是超級快。

+0

謝謝你的工作很好,除了你添加了額外的反斜槓 – ThomasReggi

+0

結尾斜槓是爲了使它不匹配從博客或投資組合開始的不同uris。我懷疑這可能發生,雖然大聲笑 感謝您選擇我的答案,而不是其他人。 – 2011-09-02 01:25:28