0
我創造了這個函數從字符串中刪除特定變量的URL指定的變量PHP函數刪除
if(!function_exists("remove_variable")) {
function remove_variable($remove = array(), $url) {
if($url == '') {
$url = $_SERVER["REQUEST_URI"];
}
foreach($remove as $r) {
echo $r.'<br>';
$url = preg_replace('/([?&])'.$r.'=[^&]+(&|$)/','$1', $url);
echo $url.'<br><br>';
}
return $url;
}
}
我與測試:
<?php echo remove_variable(array("productsearch_name", "productsearch_type", "productsearch_supplier"), $_SERVER["REQUEST_URI"].uri_glue()); ?>
這是返回:
productsearch_name
/companies/customers/pricelist?productsearch_name=&productsearch_type=Broadband%20&productsearch_supplier=&
productsearch_type
/companies/customers/pricelist?productsearch_name=&productsearch_supplier=&
productsearch_supplier
/companies/customers/pricelist?productsearch_name=&productsearch_supplier=&
/companies/customers/pricelist?productsearch_name=&productsearch_supplier=&
因此,它不會按預期去除變量
我會使用'parse_str'不是正則表達式 – 2016-12-05 20:50:17
然後''array_diff_key()'與你想刪除的數組。 – AbraCadaver
@Dagon你會如何使用'parse_str'? – charlie