Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [php] URL i wyrażenia regularne
lukasz91
post
Post #1





Grupa: Zarejestrowani
Postów: 149
Pomógł: 7
Dołączył: 6.01.2010
Skąd: Opole Lubelskie

Ostrzeżenie: (0%)
-----


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

Ten post edytował lukasz91 26.12.2010, 13:53:12


--------------------
Go to the top of the page
+Quote Post
Athlan
post
Post #2





Grupa: Developerzy
Postów: 823
Pomógł: 12
Dołączył: 18.12.2005

Ostrzeżenie: (0%)
-----


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.


--------------------
Portfolio: Vgroup.pl | athlan.pl | Test.php.pl - sprawdź się z wiedzy o PHP i ułóż własne pytania!
Pomogłem? Kliknij pod postem.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 19.08.2025 - 04:27