Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php] URL i wyrażenia regularne
Forum PHP.pl > Forum > PHP
lukasz91
Mam taki kod funkcji:
  1. /*
  2.   * It checks if string is an URL.
  3.   *
  4.   * @param string $string
  5.   * @param string $flags
  6.   * Posibility flags:
  7.   * HOST_REQUIRED - URL with a host (example: http://url.com),
  8.   * PATH_REQUIRED - URL with path after a name of domain (example: http://url.com/Home)
  9.   * QUERY_REQUIRED - URL with a Query String (example: http://url.com/index.php?x=y).
  10.   * @return boolean
  11.   */
  12. function isURL($string, $flags='QUERY_REQUIRED') {
  13. if($flags=='HOST_REQUIRED') {
  14. return (bool)preg_match("/^(http|https|ftp|file):\/\/(www)?[a-zA-Z0-9_\/.\-]+\.[a-zA-Z0-9]+(\/)?$/i", $string);
  15. }
  16. else if($flags=='PATH_REQUIRED') {
  17. return (bool)preg_match("/^(http|https|ftp|file):\/\/(www)?[a-zA-Z0-9_\/.\-]+\.[a-zA-Z0-9]+\/[a-zA-Z0-9_.\-#]*$/i", $string);
  18. }
  19. else if($flags=='QUERY_REQUIRED') {
  20. return (bool)preg_match("/^(http|https|ftp|file):\/\/(www)?[a-zA-Z0-9_\/.\-]+\.[a-zA-Z0-9]+\/[a-zA-Z0-9_.\-#]*\?[a-zA-Z0-9_.,\-?#=&]+$/i", $string);
  21. }


W jaki sposób pozbyć się możliwości napisania 2 kropek obok siebie? Prosiłbym również o spr. czy nie ma tu innych błędów. smile.gif
Athlan
Wydaje mi się, że Twoje wszystkie problemy rozwiąże parse_url sprawdzając odpowiednie elementy, które zostają zwrócone w tablicy (patrząc na Twoje flagi).

Pomimo zaleceń:
Cytat
This function is not meant to validate the given URL, it only breaks it up into the above listed parts. Partial URLs are also accepted, parse_url() tries its best to parse them correctly


Działa bardzo dobrze.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.