duanliang5051 2015-02-02 14:51
浏览 26

使用PHP explode从$ _GET创建一个单词数组

I'm having trouble using explode() in php. I want to make an array of strings from the $_GET super global array.

The url will be like:

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

I want an array of the keywords so it should be like this:

myArray(6) = { [0]=> string(4) "this"
               [1]=> string(2) "is"
               [2]=> string(2) "an"
               [3]=> string(7) "example" }

Here's my code:

$stringVals = explode("+",($_GET['keywords']));
var_dump($stringVals);

Here's the output:

array(1) { [0]=> string(30) "this is an example of a string" }

An example that works:

$pizza  = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
var_dump($pieces);

The output of this:

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" } 

I want the words from $_GET like that..

  • 写回答

6条回答 默认 最新

  • dongtiao2976 2015-02-02 14:54
    关注

    Actually you can simply use:

    explode(" ", $_GET['string'])
    

    The + sign in the url actually means a space, not plus :- ) It's because spaces aren't allowed in the urls (url cannot have whitespaces), so it's actually converted to a plus sign.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么