fork download
  1. <?php
  2.  
  3. $url_input = 'https://e...content-available-to-author-only...e.com/page/ゴッホ?search=星月夜&limit=20&offset=0';
  4. var_dump(is_url($url_input,true));
  5.  
  6. // 検証
  7. function is_url( $url, $encode = true ){
  8. if( $encode ){
  9. $url = my_urlencode($url);
  10. }
  11. return false !== filter_var($url, FILTER_VALIDATE_URL) && preg_match("[^https?+://]i", $url);
  12. }
  13.  
  14. // 検証のために必要箇所をエンコード
  15. function my_urlencode($url){
  16. $parsed = parse_url($url);
  17.  
  18. // パスをエンコード
  19. $path = $parsed['path'] ?? '';
  20. $path = rtrim($path,'/'); // 末尾スラッシュなしで統一
  21. $path_arr = explode('/',$path);
  22. $path_str = '';
  23. foreach($path_arr as $path){
  24. $path_str .= urlencode($path) . '/';
  25. }
  26. $parsed['path'] = rtrim($path_str,'/');
  27.  
  28. // パラをエンコード
  29. $query = $parsed['query'] ?? '';
  30. $para_info = [];
  31. if( $query ){
  32. parse_str($query, $para_info); // 連想配列にして重複を削除
  33. ksort($para_info); // ソート
  34. }
  35. $para_str = '';
  36. foreach($para_info as $k => $para){
  37. $para_str .= $k . '=' . urlencode($para) . '&';
  38. }
  39. $parsed['query'] = rtrim($para_str,'&');
  40.  
  41. // 構築
  42. $built = build_url($parsed);
  43. if( $path === '' ){
  44. $built = rtrim($built,'/');
  45. }
  46. if( $query === '' ){
  47. $built = rtrim($built,'?');
  48. }
  49. return $built;
  50. }
  51.  
  52. // parse_url の逆
  53. function build_url(array $elements) {
  54. $e = $elements;
  55. return
  56. (isset($e['host']) ? (
  57. (isset($e['scheme']) ? "$e[scheme]://" : '//') .
  58. (isset($e['user']) ? $e['user'] . (isset($e['pass']) ? ":$e[pass]" : '') . '@' : '') .
  59. $e['host'] .
  60. (isset($e['port']) ? ":$e[port]" : '')
  61. ) : '') .
  62. (isset($e['path']) ? $e['path'] : '/') .
  63. (isset($e['query']) ? '?' . (is_array($e['query']) ? http_build_query($e['query'], '', '&') : $e['query']) : '') .
  64. (isset($e['fragment']) ? "#$e[fragment]" : '')
  65. ;
  66. }
Success #stdin #stdout 0.02s 24620KB
stdin
Standard input is empty
stdout
bool(true)