Url.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. class Url{
  3. public static $exclude = array();
  4. public static function getParams(){
  5. $get = $_GET;
  6. if(self::$exclude){
  7. foreach(self::$exclude as $val){
  8. unset($get[$val]);
  9. }
  10. }
  11. return $get;
  12. }
  13. public static function setParams($params = array()){
  14. if($params){
  15. $get = self::getParams();
  16. $url = explode("?", $_SERVER["REQUEST_URI"], 2);
  17. $append = "";
  18. $get = array_merge($get, $params);
  19. foreach($get as $key => $val){
  20. if(is_array($val))
  21. continue;
  22. $key = trim($key);
  23. $val = trim($val);
  24. if(empty($key))
  25. continue;
  26. $append .= "&{$key}={$val}";
  27. }
  28. return "{$url[0]}?".substr($append, 1);
  29. }
  30. return $_SERVER["REQUEST_URI"];
  31. }
  32. public static function delParams($params = array(), $withSymbol = false){
  33. if($params){
  34. $get = self::getParams();
  35. $url = explode("?", $_SERVER["REQUEST_URI"], 2);
  36. $append = "";
  37. foreach($params as $k){
  38. if(Str::isBlank($k))
  39. continue;
  40. if(isset($get[$k]))
  41. unset($get[$k]);
  42. }
  43. if($get){
  44. foreach($get as $key => $val){
  45. $append .= "&{$key}={$val}";
  46. }
  47. }
  48. $url = $append ? "{$url[0]}?".substr($append, 1)."&" : "{$url[0]}?";
  49. }
  50. else{
  51. $url = self::getUrl().self::getSymbol();
  52. }
  53. if(!$withSymbol)
  54. $url = substr($url, 0, -1);
  55. return $url;
  56. }
  57. public static function delParam($param){
  58. return self::delParams(array($param));
  59. }
  60. public static function clean(){
  61. $get = self::getParams();
  62. if($get){
  63. $url = explode("?", $_SERVER["REQUEST_URI"], 2);
  64. $append = "";
  65. $count = 0;
  66. foreach($get as $key => $val){
  67. if(!is_string($val))
  68. continue;
  69. if(trim($val) === "")
  70. $count++;
  71. else
  72. $append .= "&{$key}={$val}";
  73. }
  74. $url = $append ? "{$url[0]}?".substr($append, 1) : $url[0];
  75. if($count)
  76. header("location: {$url}");
  77. }
  78. }
  79. public static function getPath(){
  80. $url = explode("?", $_SERVER["REQUEST_URI"], 2);
  81. return $url[0];
  82. }
  83. public static function getAppend(){
  84. $append = "";
  85. if($get = self::getParams()){
  86. foreach($get as $key => $val){
  87. if(is_array($val))
  88. continue;
  89. $append .= "&{$key}={$val}";
  90. }
  91. $append = "?".substr($append, 1);
  92. }
  93. return $append;
  94. }
  95. public static function getUrl(){
  96. return self::getPath().self::getAppend();
  97. }
  98. public static function getSymbol(){
  99. return strpos($_SERVER["REQUEST_URI"], "?") === false ? "?" : "&";
  100. }
  101. }