4

I have a weird project I'm working on. I'm totally new to php so it has been a struggle so far.

I have a form that gives an array and posts it:

...
return($keywords);
}

$keywordlist = explode("\r\n", $keywordlist);
foreach($keywordlist as $keyword){
print_r(mixer(strtolower($keyword)));
}

I get this:

Array ( [0] => one [1] => two [2] => three [3] => four [4] => five [5] => six [6] => ....

But would like it to come out like this instead:

%28one%2Ctwo%2Cthree%2Cfour%2Cfive%2Csix%29

Ultimately hope to be able to attach it to the end of a url like ask.com search:

"http://www.ask.com/web?q=(put my keywords here)"

then navigate there

In theory it would be like I typed "(one,two,three,four,five,six)" into the search bar and pressed enter.

Hopefully that makes some sense.

1
  • and use urlencode function to encode special characters such as &, ? and other characters to be used inside query strings. Commented May 25, 2011 at 8:46

7 Answers 7

7

Something like this:

$commaSeparated = implode(',', $array);
$withParens = '(' + $commaSeparated + ')';
$urlEncoded = urlencode($withParens);
print $urlEncoded;
Sign up to request clarification or add additional context in comments.

Comments

3

Use the php implode() function.

So you could do this:

$array = new array ( [0] => one [1] => two [2] => three [3] => four [4] => five [5] => six );
$string = '%28'.implode( '%2C', $array ).'%29';

Now $string will be what you need

Comments

2

This code:

$keywords = array('one', 'two', 'three');
$query = '(' . implode(',', $keywords) . ')';
echo('Search query: ' . $query . PHP_EOL);
$query = rawurlencode($query);
echo('Encoded: ' . $query . PHP_EOL);

Gives this output:

Search query: (one,two,three) Encoded: %28one%2Ctwo%2Cthree%29

Comments

2
$keywordlist = explode("\r\n", $keywordlist);    
array_walk($keywordlist, 'strtolower');
$keywords = '('.implode(",", $keywordList).')';

Comments

2

print_ris what prints your array like this.

http://php.net/manual/en/function.print-r.php

How to solve it depends on a few things. What does mixer()do? And is the keywords always in lowercase? If mixer doens't do much and the keywords are in lowercase you could do something like:

$string = '%28' . implode('%2C', $keyword) . '%29';

If it's URL encoding you are after you could use the function url_encode instead of manually adding encoded values as above.

Comments

2

Something like this?

$input = array('one', 'two', 'three');
$s = implode(',', $input);
$s = '('.$i.')';
print urlencode($s);

Comments

1

You could encode each part of the array using urlencode and then manually put it in your url (making a url string by yourself). http://php.net/manual/en/function.urlencode.php

Or you could use this function instead: http://php.net/manual/en/function.http-build-query.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.