A modest collection of PHP libraries used at SparkFun.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
1.6 KiB

  1. <?php
  2. namespace SparkLib;
  3. /**
  4. * Model the components of a URL as a simple object.
  5. */
  6. class URLParser {
  7. protected $_parsed = false;
  8. protected $_string = '';
  9. protected $_components = array();
  10. protected $_req = array();
  11. // to be used to detect search engines. in preg format.
  12. protected $_searchEngines = array(
  13. '.*google\.*',
  14. '.*bing\.com.*',
  15. '.*ask\.com.*',
  16. '.*search\.msn\.*',
  17. '.*search\.yahoo.*',
  18. );
  19. public function __construct ($url)
  20. {
  21. $this->_string = $url;
  22. if (is_array($this->_components = parse_url($this->_string))) {
  23. $this->_parsed = true;
  24. if (isset($this->_components['query'])) {
  25. parse_str($this->_components['query'], $this->_req);
  26. }
  27. }
  28. }
  29. /**
  30. * Were we successful at parsing a URL string?
  31. */
  32. public function parsed () { return $this->_parsed; }
  33. /**
  34. * Just get at the original URL string.
  35. */
  36. public function string () { return $this->_string; }
  37. /**
  38. * Get components of the URL.
  39. */
  40. public function __get ($key)
  41. {
  42. return $this->_components[$key];
  43. }
  44. /**
  45. * Get an object representing the GET request params, if PHP could parse
  46. * any, of the URL.
  47. */
  48. public function req ()
  49. {
  50. return (object)$this->_req;
  51. }
  52. /**
  53. * Get request GET params as an array, if we have any.
  54. */
  55. public function getReqArray()
  56. {
  57. return $this->_req;
  58. }
  59. /**
  60. * Do we think this looks like a search engine?
  61. */
  62. public function isSearchEngine ()
  63. {
  64. $search_re = '{https?://(' . implode('|', $this->_searchEngines) . ')}i';
  65. if (preg_match($search_re, $this->_string)) {
  66. return true;
  67. }
  68. return false;
  69. }
  70. }