php更改url的数组格式

发布于 2024-11-09 17:53:15 字数 679 浏览 2 评论 0原文

我正在做一个奇怪的项目。我对 php 完全陌生,所以到目前为止一直很困难。

我有一个提供数组并发布它的表单:

...
return($keywords);
}

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

我明白了:

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

但希望它像这样出现:

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

最终希望能够将其附加到像ask.com搜索这样的网址末尾:

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

然后导航到那里

理论上,就像我在搜索栏中输入“(一、二、三、四、五、六)”并按 Enter 键。

希望这是有道理的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

怪我太投入 2024-11-16 17:53:15

像这样的东西:

$commaSeparated = implode(',', $array);
$withParens = '(' + $commaSeparated + ')';
$urlEncoded = urlencode($withParens);
print $urlEncoded;

Something like this:

$commaSeparated = implode(',', $array);
$withParens = '(' + $commaSeparated + ')';
$urlEncoded = urlencode($withParens);
print $urlEncoded;
浅黛梨妆こ 2024-11-16 17:53:15

使用 php implode() 函数。

所以你可以这样做:

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

现在 $string 将是你所需要的

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

提笔书几行 2024-11-16 17:53:15

此代码:

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

给出此输出:

搜索查询:(一、二、三)编码:
%28一%2C二%2C三%29

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

源来凯始玺欢你 2024-11-16 17:53:15
$keywordlist = explode("\r\n", $keywordlist);    
array_walk($keywordlist, 'strtolower');
$keywords = '('.implode(",", $keywordList).')';
$keywordlist = explode("\r\n", $keywordlist);    
array_walk($keywordlist, 'strtolower');
$keywords = '('.implode(",", $keywordList).')';
勿挽旧人 2024-11-16 17:53:15

print_r 就是这样打印你的数组的。

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

如何解决这个问题取决于几个因素。 mixer() 的作用是什么?关键字总是小写吗?如果混频器没有做太多事情并且关键字是小写的,你可以这样做:

$string = '%28' 。内爆('%2C',
$关键字)。 “%29”;

如果它是 URL 编码,您可以使用函数 url_encode 而不是像上面那样手动添加编码值。

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.

残月升风 2024-11-16 17:53:15

像这样的东西吗?

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

Something like this?

$input = array('one', 'two', 'three');
$s = implode(',', $input);
$s = '('.$i.')';
print urlencode($s);
岁月如刀 2024-11-16 17:53:15

您可以使用 urlencode 对数组的每个部分进行编码,然后手动将其放入您的 url 中(自己制作一个 url 字符串)。 http://php.net/manual/en/function.urlencode.php

或者您可以使用此函数: http://php.net/manual /en/function.http-build-query.php

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文