Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Ive used explode on a user input variable to get an array of search terms. These search terms need to be added to the end of a URL in the following style = www.example.com/$search[0]%20$search[1]

However, I dont know how many search terms theres going to be, eg there could be 5, or there could be 1, and if I add 5 search[] variables to the end and the user inputs 1 search term, I get 4 errors for the missing variables.

Any ideas how I can solve this problem?

Thanks.

share|improve this question
    
implode()? –  j08691 Feb 7 '12 at 22:13

5 Answers 5

The opposite of explode is implode. Use that.

share|improve this answer
$url .= implode("%20", $search);

That's really all there is to it.

share|improve this answer

use the implode function with %20 as the delimiter, and your params array as the data.

this will return a string that consists of all the array values concatenated with %20 in between them

share|improve this answer

PHP already comes with a handy function to build query strings.. look up http_build_query

share|improve this answer

Use isset() to check if the variable exists. It also works on arrays. See the manual: http://www.php.net/manual/en/function.isset.php

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.