我是否需要使用 substr() 来调出一个目录,或者我可以使用其他代码吗?
该文件中的所有内容都是以 $_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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您仍然可以使用 substr,超过 10 个字符没有问题:) 换句话说,您可以完全使用您发布的代码(如果您不介意输入):
else if (substr( $_GET['q'], 0, 30) == 'ultimate-deals-and-low-prices/')
但是,如果所有文件夹都有一些共同的逻辑(例如,
cd
-进入其中或在其中搜索文件)您不必为每个文件夹编写 else 分支,您可以使用类似以下内容的内容:如果您想编写更多代码,则可以使用以下代码:会更容易理解你想要做什么。
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 you would write more of your code it'd be easier to understand what you're trying to do.
如果要使用多个关键字,您可以使用检测系统。
You could use a detection system if you are going to use multiple keywords.
strpos 到底发生了什么?
打字次数更少,不易出错,速度更快。
Whatever happened to strpos?
less to type, less error-prone, faster.