php 列出并爆炸
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2357986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
list and explode
提问by cskwrd
I am trying to use url rewriting on my website and I want to use the list()and explode()functions to get the right content.
Currently my code looks like this:
我正在尝试在我的网站上使用 url 重写,并且我想使用list()和explode()函数来获取正确的内容。目前我的代码如下所示:
list($dir, $act) = explode('/',$url);
In this case $urlis equal to everything after the first slash in the absolute url i.e. http://example.com/random/stuff => $url = random/stuff/this would work fine, but if I want to go to http://example.com/random/then it will print a notice on the page. How do I stop the notice from showing up do I need to use something other than the list()function?
在这种情况下$url,等于绝对 url 中第一个斜杠之后的所有内容,即http://example.com/random/stuff => $url = random/stuff/这可以正常工作,但如果我想去,http://example.com/random/它将在页面上打印一条通知。如何阻止通知出现我需要使用list()功能以外的东西吗?
Right now the notice is "Notice: Undefined offset: 1..."
现在通知是“注意:未定义偏移量:1 ...”
Thanks for the help!
谢谢您的帮助!
回答by Snoozer
Try this :
尝试这个 :
list($dir, $act) = array_pad(explode('/',$url), 2, '');
array_padcomplete the array with your values, here is : ''.
array_pad用您的值完成数组,这里是 : ''。
回答by Gumbo
You should check how many path segments the URL contains:
您应该检查 URL 包含多少个路径段:
$segments = explode('/', $url);
if (count($segments) !== 2) {
// error
} else {
list($dir, $act) = $segments;
}
But maybe you should choose a more flexible approach than using list.
但也许您应该选择一种比使用list.
回答by Pascal MARTIN
A solution would be to split your line of code in several, to ensure you never assign non-existing values to variables -- which is what you are doing when explodeonly returns one portion of URL.
一种解决方案是将您的代码行分成几行,以确保您永远不会将不存在的值分配给变量——这就是您explode只返回 URL 的一部分时所做的。
For that, not using listseems like the right solution, as, with list, you must know how many elements the expression on the right of =will return...
And, in this situation, you don't know how many elements explodewill return.
为此,不使用list似乎是正确的解决方案,因为对于列表,您必须知道右侧的表达式=将返回多少个元素...
而且,在这种情况下,您不知道explode将返回多少个元素。
For instance, something like this might be OK :
例如,这样的事情可能没问题:
$parts = explode('/', $url);
if (isset($parts[0])) {
$dir = $parts[0];
if (isset($parts[1])) {
$act = $parts[1];
}
}
Of course, up to you to deal with the situation in which $dirand/or $actare not set, later in your script.
当然,由您来处理$dir和/或未$act设置的情况,稍后在您的脚本中。
Another solution would be to check how many elements explode will return (counting a number of /for instance); but you'll still have to deal with at least two cases.
另一种解决方案是检查将返回多少个元素爆炸(例如计算数量/);但你仍然需要处理至少两种情况。
回答by Mathieu
to get rid of the notice:
摆脱通知:
list($dir, $act) = explode('/',$url);
but maybe a better solution would be:
但也许更好的解决方案是:
$segments = explode ('/', $url);
$dir = array_shift ($segments);
$act = array_shift ($segments);
if there is no 2nd segment, $act would be null and you can also more than 2 segment this way
如果没有第二段, $act 将为空,你也可以通过这种方式多于 2 个段
回答by giosh94mhz
The correct way to suppress the E_NOTICE on list assignment as of PHP 5.4+ is the following:
从 PHP 5.4+ 开始,在列表赋值中抑制 E_NOTICE 的正确方法如下:
@list($dir, $act) = explode('/', $url);
If $urlcontains no /, explode will return one element and $actwill be NULL.
如果不$url包含/,explode 将返回一个元素,并且$act是NULL。
回答by outis
Instead of exploding the full URL, try $_SERVER['PATH_INFO'], assuming '/random' names the script.
$_SERVER['PATH_INFO']假设 '/random' 为脚本命名,而不是分解完整的 URL,而是尝试。
回答by Luke
The simple and ugly answer is to prefix the whole thing with a single '@' which suppresses error outputs. $act will be set to null in that case because under the hood, it's equivalent to:
简单而丑陋的答案是在整个事物前面加上一个“@”来抑制错误输出。在这种情况下,$act 将被设置为 null,因为在幕后,它相当于:
$foo = explode('/',$url);
$dir = $foo[0];
$act = $foo[1]; // This is where the notice comes from

