This article describes in detail how to use regular expressions to check whether the input content is a Web site, if you are interested, you can refer to js regular expressions to check whether the input is a web site. This is also very common in Web pages. When you enter the personal homepage in the links and forms, use JavaScript to verify whether the URL is used.
This test is hard to write. It is best to use a regular expression for authentication.
It is specified that the entered content must start with http: // and https: //, and must start with a web site.
Some people say, why does a webpage like www.1.com fail?
This is to prevent the href attribute in the tag from failing to meet http: // or https: // when you construct a hyperlink using the user input, it will be considered as the root directory, and will be written to this address after the URL of your website. This should be known to everyone. For example, in xxx, my website is http: // localhost. After you click the tag that is displayed as xxx, you only need to jump to the http: // localhost/www.1.com location, of course not.
For example, in the text box below, how can we use a regular expression to require users to enter URLs starting with http: // and https?
1. The first is a simple layout. Needless to say:
Untitled document The URL must start with http: // or https: // and must be a URL named "^_^!
OK
2. The second is the script, which is not required. The key is the regular expression:
Script function CheckUrl () {var url = document. getElementById ("url "). value; var reg =/^ ([hH] [tT] {2} [pP]: \/| [hH] [tT] {2} [pP] [sS]: \/) ([A-Za-z0-9-~] +) \.) + ([A-Za-z0-9 -~ \/]) + $/; If (! Reg. test (url) {alert ("this url does not start with http: // https: // or is not a url! ") ;}Else {alert (" input successful ") ;}} script
In: var reg =/^ ([hH] [tT] {2} [pP]: \/| [hH] [tT] {2} [pP] [sS]: \/) ([A-Za-z0-9-~] +) \.) + ([A-Za-z0-9 -~ \/]) + $,
1. In Javascript, because all variables are var, the regular expression must be written in two slashes,/.../, and then the slash/in the regular expression must be written \/
2. ^ indicates that ...... [] Indicates a unit of test, that is, something that a character can hold, such as ^ ([hH] [tT] {2} [pP]: \/| [hH] [tT] {2} [pP] [sS]: \/), that is, the request must be http: // or https: // start. | Yes or, the first character is h or H, and the second and third characters are [tT], {2} indicates that the character and the first character after it must both be [tT], and so on.
3, ([A-Za-z0-9-~] +) Indicates that uppercase letters, lowercase letters, numbers, minus signs (-), or ~ are required for this character and its subsequent characters ~
The character + indicates that the character before the plus sign is matched once or n times. For example: /a +/match all 'A' in "candy" and "caaaaaaandy '.
4. Therefore ([A-Za-z0-9-~] +) \.) + Indicates XXX. The thing ending with a point must be in ([A-Za-z0-9 -~ \/]) + $ This character must appear at least once
5. $ indicates that uppercase letters, lowercase letters, numbers, minus signs (-), and ~ are required -,~ ,/End
The above is all the content of this article. I hope it will help you learn and support PHP.
More js uses regular expressions to check whether the input content is a Web site. For more information, see PHP!