Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP explode从$\u GET创建单词数组_Php_Arrays_Explode - Fatal编程技术网

使用PHP explode从$\u GET创建单词数组

使用PHP explode从$\u GET创建单词数组,php,arrays,explode,Php,Arrays,Explode,我在php中使用explode()时遇到问题。 我想从$\u GET超级全局数组中创建一个字符串数组 url将类似于: example/myproject.php?keywords=this+is+an+example 我想要一个关键字数组,所以它应该是这样的: myArray(6) = { [0]=> string(4) "this" [1]=> string(2) "is" [2]=> string(2) "an"

我在php中使用explode()时遇到问题。 我想从
$\u GET
超级全局数组中创建一个字符串数组

url将类似于:

example/myproject.php?keywords=this+is+an+example

我想要一个关键字数组,所以它应该是这样的:

myArray(6) = { [0]=> string(4) "this"
               [1]=> string(2) "is"
               [2]=> string(2) "an"
               [3]=> string(7) "example" }
这是我的密码:

$stringVals = explode("+",($_GET['keywords']));
var_dump($stringVals);
以下是输出:

array(1) { [0]=> string(30) "this is an example of a string" }
一个有效的例子:

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
var_dump($pieces);
该系统的输出:

array(6) { [0]=> string(6) "piece1" [1]=> string(6) "piece2" [2]=>
string(6) "piece3" [3]=> string(6) "piece4" [4]=> string(6) "piece5"
[5]=> string(6) "piece6" } 

我想要
$\u GET
中的单词,就像那样..

实际上,您可以简单地使用:

explode(" ", $_GET['string'])
url中的+符号实际上表示空格,而不是加号:-)
这是因为url中不允许使用空格(url不能有空格),所以它实际上被转换为加号。

在正常的GET请求中,url中的
+
将由web服务器转换回空格,因此您应该在
'
上展开

$stringVars = explode(' ', $_GET['keywords']);
请参阅以了解为什么会出现这种情况。

您看到的“+”符号实际上只是一个编码的空格。因此,可以使用空格将其正常拆分

explode(' ', $_GET['keywords']);
如果要将其放入数据库,请确保对其进行清理

$myarray = explode(" ", $_GET['keywords']);
var_dump($myArray);

怎么样?

不要使用加号,因为您看到的“+”号实际上只是一个编码的空格。在URL中使用逗号,同时将值从一个页面传递到另一个页面。在URL中使用逗号分隔的形式发送值后,解决方案如下:-

$myArray = explode(',', $_REQUEST['keywords']);
在此之后,您可以获得以下数据

$myArray[0]=this;
$myArray[1]=is;
$myArray[2]=an;
$myArray[3]=example;

首先解析url,然后删除关键字=,然后分解+号留下的内容。

$\u GET包含解码后的url。您在$\u get中获得的不是您在浏览器地址栏中看到的
var\u dump($\u GET)
会向您展示这一点。谢谢您,真不敢相信会这么简单。。。因此,如果explode()无法识别分隔符,它只会将整个字符串放入第一个数组元素中?From:/*不包含分隔符的字符串只会返回原始字符串的一个长度数组*/
$url = 'example/myproject.php?keywords=this+is+an+example';
$x = parse_url($url);
$y  = str_replace("keywords=", "", $x["query"]);

var_dump(explode("+", $y));