Php 如何解析jQuery地址插件
我正在使用插件并尝试解析一些url,这是我得到的:Php 如何解析jQuery地址插件,php,jquery-plugins,jquery,Php,Jquery Plugins,Jquery,我正在使用插件并尝试解析一些url,这是我得到的: <script>$(function(){ $.address.init(function(event) { // Initializes the plugin $('a').address(); }).change(function(event) { $('a').attr('href').replace(/^#/, '')
<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>
因此,在ajax请求完成后,它会回显:
/items.php?id=2
/items.php?id=3
我如何解析它以便只获取var值?最好是在客户端进行?
如果我的html href类似于
jQuery地址将忽略&var=jj
cheers您可以使用获取查询字符串,然后将查询字符串解析为变量
例如:
<?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'
谢谢你,我试过了,但没有成功。
/items.php?id=2
/items.php?id=3
<?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'