0

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..

  • http://stackoverflow.com/questions/11979290/is-it-possible-to-preserve-plus-signs-in-php-get-vars-without-encoding – chris85 Feb 02 '15 at 14:55
  • 3
    $_GET contains the decoded url. What you get in $_GET is **NOT** what you see in your browser's address bar. `var_dump($_GET)` would have shown you this. – Marc B Feb 02 '15 at 14:59

6 Answers6

3

The "+" sign you see is actually just an encoded space. Therefore, you can split it normally using a space.

explode(' ', $_GET['keywords']);

Make sure you sanitize it if you're going to put it in a database.

server_kitten
  • 386
  • 3
  • 13
  • Thank you, can't believe it was that simple! ... So if explode() doesn't recognize the delimiter it just puts the whole string into the first array element? – user3059460 Feb 02 '15 at 14:59
  • From http://php.net/manual/en/function.explode.php: /* A string that doesn't contain the delimiter will simply return a one-length array of the original string. */ – user3059460 Feb 02 '15 at 15:02
2

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.

Rav
  • 1,460
  • 1
  • 21
  • 33
0

In a normal GET request, the + in the URL will be converted back to spaces by the web server, so you should be exploding on ' '.

$stringVars = explode(' ', $_GET['keywords']);

See https://stackoverflow.com/a/2678602/1331451 for an explanation of why that is the case.

Community
  • 1
  • 1
simbabque
  • 53,749
  • 8
  • 73
  • 136
0
$myarray = explode(" ", $_GET['keywords']);
var_dump($myArray);

How's that?

Peter B
  • 437
  • 4
  • 14
0

Don't use plus symbol because The "+" sign you see is actually just an encoded space. use comma in URL while passing values from one page to another page here is solution after sending them in URL using comma separated form :-

$myArray = explode(',', $_REQUEST['keywords']);

after this you can get your data as following

$myArray[0]=this;
$myArray[1]=is;
$myArray[2]=an;
$myArray[3]=example;
sunena
  • 81
  • 7
0
$url = 'example/myproject.php?keywords=this+is+an+example';
$x = parse_url($url);
$y  = str_replace("keywords=", "", $x["query"]);

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

First parse the url, second remove keywords=, next explode what's left by + sign.