关于#php#根据url判断选择加载模板应该如何解决?会的速来捡钱!

拿到一套php程序,现在需要加几个判断,根据url中的参数决定打开的模板文件
目前运行没有错误,可以正常输出!
    function read_templates(){
    if($_SERVER['REQUEST_URI']=='/'||$_SERVER['REQUEST_URI']==''){
            $templates_content = file_get_contents('data/templates/index.html');

    }else{
        global $isspider;
          if($isspider==1) {
              
                  $templates_content = file_get_contents('data/templates/spider.html');
          }else{
            $templates_content = file_get_contents('data/templates/show.html');
          }
    }
        
        return $templates_content;
    }

目前没有报错!
自己不会写!
我想要 在$isspider 中添加判断:

1、在url中含有 “v-123.html” 则调用 v的模板,含有 “w-abc.html” 则调用 w的模板(v-和w-后面是表示任意字符和数字,所以只能能用v-或w-做判断)
2、若url中不含有 v-或w- 则指向一个域名或页面均可!

判断后能正常输出就行,会的相当于捡钱!佣金只给首位提供正确答案者!


function read_templates(){
    if($_SERVER['REQUEST_URI']=='/'||$_SERVER['REQUEST_URI']==''){
        $templates_content = file_get_contents('data/templates/index.html');
 
    }else{
        global $isspider;
        if(preg_match('/v-[\w]+\.html/', $_SERVER['REQUEST_URI'])) {
            $templates_content = file_get_contents('data/templates/v.html');
        }else if(preg_match('/w-[\w]+\.html/', $_SERVER['REQUEST_URI'])) {
            $templates_content = file_get_contents('data/templates/w.html');
        }else if($isspider==1) {
            $templates_content = file_get_contents('data/templates/spider.html');
        }else{
            $templates_content = file_get_contents('data/templates/show.html');
        }
    }
        
    return $templates_content;
}


可以尝试使用PHP中的正则表达式来满足你的需求:

1、定义正则表达式,识别包含v-和w-前缀的URL:$regex = '/^(v-\w+|w-\w+).html$/';
2、在read_templates()函数中添加对$_SERVER['REQUEST_URI']变量的正则匹配,以判断url中是否存在v-和w-前缀的内容:

if (preg_match($regex, $_SERVER['REQUEST_URI'], $matches)) {
  // 匹配成功,根据v-或w-前缀加载不同的模板
  if ($matches[0][0] == 'v') {
    $templates_content = file_get_contents('data/templates/v.html');
  } else {
    $templates_content = file_get_contents('data/templates/w.html');
  }
} else {
  // 匹配失败,根据是否为spider加载模板
  if($isspider==1) {
    $templates_content = file_get_contents('data/templates/spider.html');
  } else {
    $templates_content = file_get_contents('data/templates/show.html');
  }
}

希望这些内容能帮助你解决问题。

如果想要在PHP中根据url判断选择加载模板,可以使用PHP的$_SERVER['REQUEST_URI']获取当前url地址,然后对不同的url进行不同的模板加载。例如:$url = $_SERVER['REQUEST_URI'];if($url == 'index.php'){ include 'index.html'; }else if($url == 'page.php'){ include 'page.html'; }这样根据url的不同,就可以加载不同的模板了。

该回答引用ChatGPT
请参考下面的解决方案,如果有帮助,还请点击 “采纳” 感谢!
请根据下面代码进行修改

function read_templates(){
    if($_SERVER['REQUEST_URI']=='/'||$_SERVER['REQUEST_URI']==''){
        $templates_content = file_get_contents('data/templates/index.html');
    }else{
        global $isspider;
        if($isspider==1) {
            $url = $_SERVER['REQUEST_URI'];
            if (strpos($url, 'v-') !== false) {
                $templates_content = file_get_contents('data/templates/v.html');
            } elseif (strpos($url, 'w-') !== false) {
                $templates_content = file_get_contents('data/templates/w.html');
            } else {
                $templates_content = file_get_contents('data/templates/spider.html');
            }
        }else{
            $templates_content = file_get_contents('data/templates/show.html');
        }
    }

    return $templates_content;
}