I have an input field, that when submitted attaches a code like this to the url:
?searchval=this+is+a+test
In my php file I now want to store this string word by word in an array, splitting it where the spaces are… Something like this:
array[0] = "this";
array[1] = "is";
array[2] = "a";
array[3] = "test";
What would be the most effective way to accomplish this? I have messed around with substr() and preg_replace() but the whole thing is very messy and I figured that there must be an easier way, considering the $_GET also splits the words with +’s in the URL.
Any help would be greatly appreciated. 🙂
You are looking for
explode.I should note here that while you may see plus signs in the URL, these are automatically translated to spaces by PHP when
$_GETis populated. In technical terms, the names and values of the query string variables are URL-decoded (exactly what you can also do in code withurldecode).