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%2开发者_如何学JAVA9
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.
Something like this:
$commaSeparated = implode(',', $array);
$withParens = '(' + $commaSeparated + ')';
$urlEncoded = urlencode($withParens);
print $urlEncoded;
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
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
$keywordlist = explode("\r\n", $keywordlist);
array_walk($keywordlist, 'strtolower');
$keywords = '('.implode(",", $keywordList).')';
print_r
is 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.
Something like this?
$input = array('one', 'two', 'three');
$s = implode(',', $input);
$s = '('.$i.')';
print urlencode($s);
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
精彩评论