2011-01-28 45 views
1

我使用http://www.asual.com/jquery/address/插件,並試圖解析某些URL,這是我的了:如何解析的jQuery插件地址

<script>$(function(){ 
     $.address.init(function(event) { 

       // Initializes the plugin 
       $('a').address(); 

      }).change(function(event) { 

$('a').attr('href').replace(/^#/, ''); 

    $.ajax({ 
    url: 'items.php', 
    data:"data="+event.value, 
    success: function(data) { 
    $('div#content').html(data) 

     } 
    }) 

}); 


})</script> 

然後HTML:

<a href="items.php?id=2">link02</a> 
<a href="items.php?id=3">link03</a> 

然後即時調用item.php ,對於至極現在只有

echo $_GET["data"]; 

所以Ajax請求有後完成它呼應:

/items.php?id=2 
/items.php?id=3 

我該如何解析這個,所以我只得到var值?最好在客戶端做到這一點? 並且如果我的HTML href是像<a href="items.php?id=2&var=jj">link02</a> jQuery的地址將忽略&var=jj

歡呼

回答

2

您可以使用parse_url()來獲取查詢字符串,然後parse_str得到解析爲變量的查詢字符串。

例子:

<?php 

// this is your url 
$url = 'items.php?id=2&var=jj'; 

// you ask parse_url to parse the url and return you only the query string from it 
$queryString = parse_url($url, PHP_URL_QUERY); 

// parse_str can extract the variables into the global space or can put them into an array 
// NOTE: for simplicity no validation is performed but in production you should perform validation 
$params = array(); 
parse_str($queryString, $params); 
// you can access the values like thid 
echo $params['id']; // will output 2 
echo $params['var']; // will output 'jj' 
// I prefer this way, because it eliminates the posibility of overwriting another global variable 
// with the same name as one of the parameters in the url. 

// or you can tell parse_str to extract the variables into the global space 
parse_str($queryString); 
// or if you want to use the global scope 
echo $id; // will output 2 
echo $var; // will output 'jj' 
+0

謝謝你,我是想這一點,但很少更迭。 – tetris 2011-01-28 09:15:05