我要求用户在文本框中输入一个URL,并需要在其中附加一个查询字符串。
网址的可能值可以是:
现在我需要像“q2 = two”一样添加查询字符串,这样输出就像:
答案 0 :(得分:34)
<?php
$urls = array(
'http://www.example.com',
'http://www.example.com/a/',
'http://www.example.com/a/?q1=one',
'http://www.example.com/a.html',
'http://www.example.com/a.html?q1=one'
);
$query = 'q2=two';
foreach($urls as &$url) {
$parsedUrl = parse_url($url);
if ($parsedUrl['path'] == null) {
$url .= '/';
}
$separator = ($parsedUrl['query'] == NULL) ? '?' : '&';
$url .= $separator . $query;
}
var_dump($urls);
array(5) {
[0]=>
string(29) "http://www.example.com/?q2=two"
[1]=>
string(32) "http://www.example.com/a/?q2=two"
[2]=>
string(39) "http://www.example.com/a/?q1=one&q2=two"
[3]=>
string(36) "http://www.example.com/a.html?q2=two"
[4]=>
&string(43) "http://www.example.com/a.html?q1=one&q2=two"
}
答案 1 :(得分:13)
$ url是您的网址。使用strpos功能
if(strpos($url,'?') !== false) {
$url .= '&q2=two';
} else {
$url .= '?q2=two';
}
答案 2 :(得分:7)
我知道这已经过时了,但我改进了alex's answer来解释&#34;#&#34;字符串的一部分。
$urls = array(
'http://www.example.com',
'http://www.example.com/a/#something',
'http://www.example.com/a/?q1=one#soe',
'http://www.example.com/a.html',
'http://www.example.com/a.html?q1=one'
);
$query = 'q2=two';
foreach($urls as &$url) {
$pound = "";
$poundPos = -1;
//Is there a #?
if ( ( $poundPos = strpos( $url, "#" ) ) !== false )
{
$pound = substr( $url, $poundPos );
$url = substr( $url, 0, $poundPos );
}
$separator = (parse_url($url, PHP_URL_QUERY) == NULL) ? '?' : '&';
$url .= $separator . $query . $pound;
}
var_dump($urls);
答案 3 :(得分:1)
如果您可以安装pecl_http,那么这是一个非常优雅的解决方案。它可以让您检查是否覆盖了他们可能设置的get变量。
$urlComps = parse_url($url);
// Get the current query string
$queryString = isset($urlComps['query']) ? $urlComps['query'] : '';
// Turn it into an array for easy manipulation
parse_str($queryString, $queryVars);
// Make changes to the query vars
$queryVars['q2'] = 'two';
// Empty paths return relative URLs.
$urlComps['path'] = isset($urlComps['path']) ? $urlComps['path'] : '/';
// Make the pecl_http call
$newURL = http_build_url($urlComps, array('query' => http_build_query($queryVars)));
注意:如果你不能安装pecl_http,那么唯一的函数是带有函数http_build_url
的最后一个函数。您可以相当轻松地构建自己的函数来重建其组件中的URL ...
答案 4 :(得分:1)
增强@ alex对帐户无限附加查询字符串
的回答/* Append QueryString to current URL */
function querystring_append($query) {
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$parsedUrl = parse_url($url);
if ($parsedUrl['path'] == null) {
$url .= '/';
}
$separator = ($parsedUrl['query'] == NULL) ? '?' : '&';
if(!substr_count($url,$query)) $url .= $separator . $query;
return $url;
}
用法:
<?=querystring_append("action=logout") ?>
答案 5 :(得分:1)
这应该是比较棘手的。 @alex答案很好,但是它不支持URL片段,也不处理重复的值。我只是为这种情况提供了一个程序包。参见https://github.com/Nyholm/append_query_string
$url = 'https://nyholm.tech?example=yes';
$queryString = http_build_query(['foo'=>'bar']);
$result = append_query_string($url, $queryString);
echo $result;
// https://nyholm.tech?example=yes&foo=bar
您还可以定义如何处理重复项。您可以决定:
答案 6 :(得分:0)
这是我使用的功能:
/**
* @param string $url
* @param $query string|array
* @return string
*/
public function appendQueryStringToURL(string $url, $query): string
{
// the query is empty, return the original url straightaway
if (empty($query)) {
return $url;
}
$parsedUrl = parse_url($url);
if (empty($parsedUrl['path'])) {
$url .= '/';
}
// if the query is array convert it to string
$queryString = is_array($query) ? http_build_query($query) : $query;
// check if there is already any query string in the URL
if (empty($parsedUrl['query'])) {
// remove duplications
parse_str($queryString, $queryStringArray);
$url .= '?' . http_build_query($queryStringArray);
} else {
$queryString = $parsedUrl['query'] . '&' . $queryString;
// remove duplications
parse_str($queryString, $queryStringArray);
// place the updated query in the original query position
$url = substr_replace($url, http_build_query($queryStringArray), strpos($url, $parsedUrl['query']), strlen($parsedUrl['query']));
}
return $url;
}
它接受query
作为string
或array
。它还处理URL中的#
并自动删除重复的查询字符串。这也是测试。请使用项目中正确的CLASS_THAT_CONTAINS_appendQueryStringToURL
替换class
:
public function testAppendQueryStringToURL()
{
$helper = new CLASS_THAT_CONTAINS_appendQueryStringToURL();
$inputsOutputs = [
[
'i' => 'http://www.example.com',
'q' => 'q1=1',
'o' => 'http://www.example.com/?q1=1'
],
[
'i' => 'http://www.example.com',
'q' => 'q1=1&q2=2',
'o' => 'http://www.example.com/?q1=1&q2=2'
],
[
'i' => 'http://www.example.com/a/',
'q' => 'q1=1',
'o' => 'http://www.example.com/a/?q1=1'
],
[
'i' => 'http://www.example.com/a.html',
'q' => 'q1=1',
'o' => 'http://www.example.com/a.html?q1=1'
],
[
'i' => 'http://www.example.com/a/?q2=2',
'q' => 'q1=1',
'o' => 'http://www.example.com/a/?q2=2&q1=1'
],
[
'i' => 'http://www.example.com/a.html?q2=two',
'q' => 'q1=1',
'o' => 'http://www.example.com/a.html?q2=two&q1=1'
],
[
'i' => 'http://www.example.com/a.html?q1=1&q2=2',
'q' => 'q1=1',
'o' => 'http://www.example.com/a.html?q1=1&q2=2'
],
// overwrite the existing
[
'i' => 'http://www.example.com/a.html?q1=1&q2=2',
'q' => 'q1=3',
'o' => 'http://www.example.com/a.html?q1=3&q2=2'
],
[
'i' => 'http://www.example.com/a/#something',
'q' => 'q1=1',
'o' => 'http://www.example.com/a/#something?q1=1'
],
[
'i' => 'http://www.example.com/a/?q2=2#soe',
'q' => 'q1=1',
'o' => 'http://www.example.com/a/?q2=2&q1=1#soe'
],
[
'i' => 'http://www.example.com',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/?q1=1'
],
[
'i' => 'http://www.example.com',
'q' => ['q1' => 1, 'q2' => 2],
'o' => 'http://www.example.com/?q1=1&q2=2'
],
[
'i' => 'http://www.example.com/a/',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/a/?q1=1'
],
[
'i' => 'http://www.example.com/a.html',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/a.html?q1=1'
],
[
'i' => 'http://www.example.com/a/?q2=2',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/a/?q2=2&q1=1'
],
[
'i' => 'http://www.example.com/a.html?q2=two',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/a.html?q2=two&q1=1'
],
[
'i' => 'http://www.example.com/a.html?q1=1&q2=2',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/a.html?q1=1&q2=2'
],
[
'i' => 'http://www.example.com/a.html?q1=1&q2=2',
'q' => ['q1' => 3],
'o' => 'http://www.example.com/a.html?q1=3&q2=2'
],
[
'i' => 'http://www.example.com/a/#something',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/a/#something?q1=1'
],
[
'i' => 'http://www.example.com/a/?q2=2#soe',
'q' => ['q1' => 1],
'o' => 'http://www.example.com/a/?q2=2&q1=1#soe'
],
];
foreach ($inputsOutputs as $inputOutput) {
$this->assertEquals($inputOutput['o'], $helper->appendQueryStringToURL($inputOutput['i'], $inputOutput['q']));
}
}
答案 7 :(得分:0)
顺便说一句,如果有人(像我一样)在 WordPress 中实现了这个功能,那么一个函数已经存在:add_query_arg()
add_query_arg( 'key', 'value', 'http://example.com' );
更多示例请参见法典 - https://developer.wordpress.org/reference/functions/add_query_arg/