我是否需要使用 substr() 来调出一个目录,或者我可以使用其他代码吗?

发布于 2024-12-11 21:10:32 字数 822 浏览 2 评论 0原文

该文件中的所有内容都是以 $_GET['q'] 调出主域名的方式设置的,而网站中的其他目录则以这种方式调出: else if (substr($_GET['q'], 0, 7) == 'quotes/') 如果我想要一个名为:ultimate-deals-and-low-prices 的文件夹,我会使用

else if (substr($_GET['q'], 0, 30) == 'ultimate-deals-and-low-prices/')

这有意义吗,或者我会以另一种方式调用我的目录,而不必要求 php 调用目录文件的子字符串姓名。我知道这就是 substring 的作用,但我只看到它用于少于 10 个字符。如果我的目录有更多字符,我还会使用 substring 吗?

这是本文档中 if 语句的开头:

require_once(getcwd() ."/db/db_pagetitle.php");
print get_page_title($_GET['q']);
if (isset($_GET['q']) && $_GET['q']) {
    if (file_exists(getcwd() .'/pages/'. $_GET['q'] .'.php')) {
        require_once(getcwd() .'/pages/'. $_GET['q'] .'.php');

然后进入“

else if (substr($_GET['q'], 0, 7) == 'quotes/') 

如何调用我的目录?”

谢谢?

Everything in this file is set up in a way in which $_GET['q'] brings up the main domain name and the other directories in the website are brought up in this manner:
else if (substr($_GET['q'], 0, 7) == 'quotes/')
If I wanted to have a folder called: ultimate-deals-and-low-prices, would I use

else if (substr($_GET['q'], 0, 30) == 'ultimate-deals-and-low-prices/')

Does that make sense, or would I call my directory in another manner, without having to ask php to call a substring of the directory file name. I understand that is what substring does, but I've only seen it be used for characters less than 10. If my directory has more characters, would I still use substring?

This is the beginning of the if statement in this document:

require_once(getcwd() ."/db/db_pagetitle.php");
print get_page_title($_GET['q']);
if (isset($_GET['q']) && $_GET['q']) {
    if (file_exists(getcwd() .'/pages/'. $_GET['q'] .'.php')) {
        require_once(getcwd() .'/pages/'. $_GET['q'] .'.php');

than it goes into

else if (substr($_GET['q'], 0, 7) == 'quotes/') 

How do I call my directory?

Thanks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫瑟鸿黎 2024-12-18 21:10:32

您仍然可以使用 substr,超过 10 个字符没有问题:) 换句话说,您可以完全使用您发布的代码(如果您不介意输入): else if (substr( $_GET['q'], 0, 30) == 'ultimate-deals-and-low-prices/')

但是,如果所有文件夹都有一些共同的逻辑(例如, cd-进入其中或在其中搜索文件)您不必为每个文件夹编写 else 分支,您可以使用类似以下内容的内容:

if (isset($_GET['q']) && $_GET['q']) {
    if (file_exists(getcwd() .'/pages/'. $_GET['q'] .'.php')) {
        require_once(getcwd() .'/pages/'. $_GET['q'] .'.php');
    } else if (is_dir(getcwd() . '/pages/' . $_GET['q'])) {
        // do whatever you want to do with the folder
    }
}

如果您想编写更多代码,则可以使用以下代码:会更容易理解你想要做什么。

You can still use substr, there's no problem with having more than 10 characters :) In other words, you can use exactly the code you've posted (if you don't mind the typing): else if (substr($_GET['q'], 0, 30) == 'ultimate-deals-and-low-prices/')

But, if there is some of common logic for all folders (like, cd-ing into it or searching for a file in it) you don't have to write an else branch for each folder, you can just use something like:

if (isset($_GET['q']) && $_GET['q']) {
    if (file_exists(getcwd() .'/pages/'. $_GET['q'] .'.php')) {
        require_once(getcwd() .'/pages/'. $_GET['q'] .'.php');
    } else if (is_dir(getcwd() . '/pages/' . $_GET['q'])) {
        // do whatever you want to do with the folder
    }
}

If you would write more of your code it'd be easier to understand what you're trying to do.

微凉徒眸意 2024-12-18 21:10:32

如果要使用多个关键字,您可以使用检测系统。

$match[]='ultimate-deals-and-low-prices/';
$match[]='users/';
$match[]='imgs/';
foreach($match as $k=>$v){
$l=strlen($v);
$s=substr($_GET[q],0,$l);
if ($v==$s) //Code to run...
}

You could use a detection system if you are going to use multiple keywords.

$match[]='ultimate-deals-and-low-prices/';
$match[]='users/';
$match[]='imgs/';
foreach($match as $k=>$v){
$l=strlen($v);
$s=substr($_GET[q],0,$l);
if ($v==$s) //Code to run...
}
桃扇骨 2024-12-18 21:10:32

strpos 到底发生了什么?

<?php

if (file_exists($sanitizedPath)) {

} elseif (strpos($_GET['q'], "ultimate-deals-and-low-prices/") === 0) {

} elseif (strpos($_GET['q'], "some-other-directory/") === 0) {

}

打字次数更少,不易出错,速度更快。

Whatever happened to strpos?

<?php

if (file_exists($sanitizedPath)) {

} elseif (strpos($_GET['q'], "ultimate-deals-and-low-prices/") === 0) {

} elseif (strpos($_GET['q'], "some-other-directory/") === 0) {

}

less to type, less error-prone, faster.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文