2011-03-18 203 views
1
<a href="http://www.google.com/search?q='.urlencode(current(explode('(', $ask_key))).'" target="_blank"> 

我不明白urlencode(current(explode('(', $ask_key)))做什么。
任何人都可以解释我的代码做什么?这段代码做了什么?

回答

1

$arraystring,它必须包含几个值,用(分隔。

explode()会将此字符串拆分为array,并使用(作为分隔符。

current()将获取数组的当前元素 - 第一个。

最后,urlencode()将对特殊字符进行编码,以便它们可以在URL中使用。


因此,基本上:

  • 以一个字符串的第一个元素,如these(are(elements
  • 应用urlencode功能,因此它可以在URL中使用。


作为一个例子,这里是相同种类的代码,分割成几个不同的操作,使用一个变量来存储每个函数的结果 - 所以我们可以转储这些结果:

$string = "[email protected]?i&s(a couple(of elements"; 
var_dump($string); 

$array = explode('(', $string); 
var_dump($array); 

$first_item = current($array); 
var_dump($first_item); 

$encoded = urlencode($first_item); 
var_dump($encoded); 

四个var_dump()会给这个输出:

string '[email protected]?i&s(a couple(of elements' (length=30) 

array 
    0 => string '[email protected]?i&s' (length=9) 
    1 => string 'a couple' (length=8) 
    2 => string 'of elements' (length=11) 

string '[email protected]?i&s' (length=9) 

string 'th%40is%3Fi%26s' (length=15) 

这都说明在细节你的表情的每一个部分呢。

+0

粗犷再次胜过简洁:) – webbiedave 2011-03-18 06:55:59

3

explode字符串$ask_key成使用(作为分隔符的阵列(因此,如果的$ask_key值为a(b(c,然后array('a', 'b', 'c')将被返回。

并抓住第一,即current(作为新的数组被指向到其第一元件)阵列的,元件

然后urlencode它(使它安全使用在查询字符串)。

+0

谢谢你,你让我知道爆炸()一样。 – enjoylife 2011-03-18 05:42:20

1
$ask_key = 'as das df(sdfkj as(asf a152451(sdfa df1 9'; //you key 

echo $ask_key."<br/>"; 

$array = explode('(', $ask_key); //explode will split the array on '(' 

echo "<pre>"; 
print_r($array); 
echo "</pre>"; 


$curr = current($array); //current will return the curr element of array 

echo $curr."<br/>"; 

$enc = urlencode($curr); //url will encode url i.e. valid url 

echo $enc; 

结果::

as das df(sdfkj as(asf a152451(sdfa df1 9 

Array 
(
    [0] => as das df 
    [1] => sdfkj as 
    [2] => asf a152451 
    [3] => sdfa df1 9 
) 

as das df 
as+das++df 
+0

很好的例子。谢谢。 – enjoylife 2011-03-18 05:45:57

+0

当然欢迎你 – 2011-03-18 05:53:01