0

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.

1
  • implode()? Commented Feb 7, 2012 at 22:13

5 Answers 5

2

The opposite of explode is implode. Use that.

Sign up to request clarification or add additional context in comments.

Comments

1
$url .= implode("%20", $search);

That's really all there is to it.

Comments

1

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

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.