PHP validation/regex for URL

后端 未结 21 2444
青春惊慌失措
青春惊慌失措 2020-11-22 01:19

I\'ve been looking for a simple regex for URLs, does anybody have one handy that works well? I didn\'t find one with the zend framework validation classes and have seen sev

21条回答
  •  Happy的楠姐
    2020-11-22 01:54

    "/(http(s?):\/\/)([a-z0-9\-]+\.)+[a-z]{2,4}(\.[a-z]{2,4})*(\/[^ ]+)*/i"
    
    1. (http(s?)://) means http:// or https://

    2. ([a-z0-9-]+.)+ => 2.0[a-z0-9-] means any a-z character or any 0-9 or (-)sign)

                   2.1 (+) means the character can be one or more ex: a1w, 
                       a9-,c559s, f)
      
                   2.2 \. is (.)sign
      
                   2.3. the (+) sign after ([a-z0-9\-]+\.) mean do 2.1,2.2,2.3 
                      at least 1 time 
                    ex: abc.defgh0.ig, aa.b.ced.f.gh. also in case www.yyy.com
      
                   3.[a-z]{2,4} mean a-z at least 2 character but not more than 
                                4 characters for check that there will not be 
                                the case 
                                ex: https://www.google.co.kr.asdsdagfsdfsf
      
                   4.(\.[a-z]{2,4})*(\/[^ ]+)* mean 
      
                     4.1 \.[a-z]{2,4} means like number 3 but start with 
                         (.)sign 
      
                     4.2 * means (\.[a-z]{2,4})can be use or not use never mind
      
                     4.3 \/ means \
                     4.4 [^ ] means any character except blank
                     4.5 (+) means do 4.3,4.4,4.5 at least 1 times
                     4.6 (*) after (\/[^ ]+) mean use 4.3 - 4.5 or not use 
                         no problem
      
                     use for case https://stackoverflow.com/posts/51441301/edit
      
                     5. when you use regex write in "/ /" so it come
      

      "/(http(s?)://)([a-z0-9-]+.)+[a-z]{2,4}(.[a-z]{2,4})(/[^ ]+)/i"

                     6. almost forgot: letter i on the back mean ignore case of 
                        Big letter or small letter ex: A same as a, SoRRy same 
                        as sorry.
      

    Note : Sorry for bad English. My country not use it well.

提交回复
热议问题