url.php ➔ UnsetUrlParam()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 4
nop 2
dl 0
loc 21
rs 9.584
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package		fwolflib
4
 * @subpackage	func
5
 * @copyright	Copyright 2003-2010, Fwolf
6
 * @author		Fwolf <[email protected]>
7
 * @since		2003 someday
8
 */
9
10
11
require_once(dirname(__FILE__) . '/../fwolflib.php');
12
13
14
/**
15
* 将数组转换为URL地址
16
*
17
* 要进行转换的源数组必须是{@link UrlToArray()}结果的格式,即数组的第一个元素为文件地址,其余为各参数
18
* @access   public
19
* @see      UrlToArray()
20
* @param    array   $ar 数组
21
* @return   string
22
*/
23 View Code Duplication
function ArrayToUrl(&$ar)
0 ignored issues
show
Duplication introduced by
This function seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
{
25
    $i = count($ar);
26
    $s_url = '';
27
    if (0 < $i)
28
    {
29
        $s_url .= $ar[0][0] . '?';
30
        for ($j = 1; $j < $i; $j++)
31
        {
32
            foreach ($ar[$j] as $key=>$val)
33
            {
34
                $s_url .= $val . '=';
35
            }
36
            $s_url = substr($s_url, 0, strlen($s_url) - 1);
37
            $s_url .= '&';
38
        }
39
        $s_url = substr($s_url, 0, strlen($s_url) - 1);
40
    }
41
    //去掉URL尾端的无效字符
42
    $s_url = str_replace('&=', '', $s_url);
43
//    $s_url = ereg_replace ('[&]+$', '', $s_url);
44
    $s_url = preg_replace ('/[&]+$/', '', $s_url);
45
    return($s_url);
46
} // end function ArrayToUrl
47
48
49
/**
50
* 增加或设置/更改URL参数
51
* @access   public
52
* @see      UnsetUrlParam()
53
* @param    string  $urlStr     要进行处理的URL地址
54
* @param    string  $strName    要添加的参数等号左边,参数名
55
* @param    string  $strValue   要添加的参数等号右边,参数值
56
* @return   string
57
*/
58
function SetUrlParam($urlStr, $strName, $strValue = '')
59
{
60
    if (empty($strName) && empty($strValue)) {
61
        return($urlStr);
62
    }
63
    $ar = UrlToArray($urlStr);
64
    $i = 1;
65
    $is_found = 0;
66 View Code Duplication
    while (count($ar) > $i)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        if ($strName == $ar[$i][0])
69
        {
70
            //已经有同名的参数了
71
            $ar[$i][1] = $strValue;
72
            $is_found ++;
73
        }
74
        $i++;
75
    }
76
    if (1 > $is_found)
77
    {
78
        //没有找到同名的参数
79
        array_push($ar, array($strName, $strValue));
80
    }
81
    return(ArrayToUrl($ar));
82
} // end function SetUrlParam
83
84
85
/**
86
 * 为指定的文字内容按照指定的规则格式化成一个链接的HTML代码,返回该HTML字符串
87
 * @access  public
88
 * @param   string  $str            要进行格式化的内容
89
 * @param   string  $linkAddress    链接地址
90
 * @param   string  $targetWindow   链接的目标窗口
91
 * @param   string  $paramStr       其他参数字符串,按照原样加到链接代码中
92
 * @return  string
93
 */
94
function ToLink($str, $linkAddress, $targetWindow = '', $paramStr = '')
95
{
96
    $s_url = '';
97
    $s_url .= '<a href="' . $linkAddress . '" ';
98
    if (!empty($targetWindow))
99
    {
100
        $s_url .= 'target="' . $targetWindow . '" ';
101
    }
102
    if (!empty($paramStr))
103
    {
104
        $s_url .= $paramStr;
105
    }
106
    $s_url .= '>' . $str . '</a>';
107
    return($s_url);
108
}
109
110
111
/**
112
* 去掉URL参数
113
* @access   public
114
* @see      SetUrlParam()
115
* @param    string  $urlStr     要进行处理的URL地址
116
* @param    string  $strName    要删除的参数名
117
* @return   string
118
*/
119
function UnsetUrlParam($urlStr, $strName)
120
{
121
    if (empty($strName))
122
    {
123
        return($urlStr);
124
    }
125
    $ar = UrlToArray($urlStr);
126
    $ar2 = array();
127
    foreach ($ar as $key=>$val)
128
    {
129
        if ($strName == $val[0])
130
        {
131
            //找到指定的参数了,因为要删除他,所有就不复制,什么都不作
132
        }
133
        else
134
        {
135
            array_push($ar2, $val);
136
        }
137
    }
138
    return(ArrayToUrl($ar2));
139
} // end function UnsetUrlParam
140
141
142
/**
143
* 将URL地址转换为数组
144
*
145
* {@source 4 21}
146
* @access   public
147
* @see      ArrayToUrl()
148
* @param    string  $urlStr URL地址
149
* @return   array
150
*/
151
function UrlToArray($urlStr) {
152
    /*
153
    示例:转换 'http://localhost/index.php?a=1&b=&c=d.php?e=5&f=6'的结果为
154
    Array(
155
        [0] => Array(
156
                [0] => http://localhost/working/hebca/source/test/index.php
157
                [1] =>)
158
        [1] => Array(
159
                [0] => a
160
                [1] => 1)
161
        [2] => Array(
162
                [0] => b
163
                [1] =>)
164
        [3] => Array(
165
                [0] => c
166
                [1] => d.php?e
167
                [2] => 5)
168
        [4] => Array(
169
                [0] => f
170
                [1] => 6) )
171
    */
172
    $ar = array();
173
    $str = $urlStr;
174
    $i = 0;
0 ignored issues
show
Unused Code introduced by
$i is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
175
    //先寻找“?”
176
    $i = strpos($str, '?');
177
    if (1 > $i) {
178
        //URL中没有?,说明其没有参数
179
        array_push($ar, array($str, ''));
180
    }
181
    else {
182
        array_push($ar, array(substr($str, 0, $i), ''));
183
        $str = substr($str, $i + 1) . '&';
184
        //解析用&间隔的参数
185
        while (!empty($str)) {
186
            $i = strpos($str, '&');
187
            if (0 < $i) {
188
                $sub_str = substr($str, 0, $i);
189
                //分析$sub_str这个等式
190
                array_push($ar, preg_split('/[=]/', $sub_str));
191
                $str = substr($str, $i + 1);
192
            }
193
            elseif ('&' == $str[0]) {
194
				$str = substr($str, 1);
195
			}
196 View Code Duplication
			else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
197
                //剩下的不可识别字符
198
                array_push($ar, array(substr($str, 0, 1), ''));
199
                $str = substr($str, 1);
200
            }
201
        }
202
    }
203
    return($ar);
204
} // end function UrlToArray
205
206
207
/**
208
 * Find url plan from url
209
 *
210
 * eg: http://www.google.com/, plan = http
211
 *
212
 * Obsolete, changed to request.php::GetUrlPlan()
213
 * @param	string	$url
214
 * @return	string
215
 */
216
function UrlPlan($url) {
217
	$i = preg_match('/^(\w+):\/\//', $url, $ar);
218
	if (1 == $i)
219
		return $ar[1];
220
	else
221
		return '';
222
} // end of func UrlPlan
223
224
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
225