-1

I have searched for the explanation/meaning of following website validation code in PHP,but didnt get it. Any clue?

(!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
Laurentiu L.
  • 6,213
  • 1
  • 28
  • 55
ArjunKasi
  • 11
  • 6
  • 1
    possible duplicate of [Reference - What does this regex mean?](http://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – nhahtdh Sep 04 '15 at 10:24
  • 1
    This question in it's current form is unclear, and if clarified it would most likely be too broad. What you really should do is read a RegEx reference, like the one mentioned by @nhahtdh. However, I don't think this question should be closed as a duplicate of that question, it should be closed as Too Broad, because in any clarified form I can imagine it would still be Too Broad for this format. –  Sep 07 '15 at 16:59

1 Answers1

0

If you google preg_match you will find a reference to a php method which performs a regular expression match.

Your code checks if $website matches the pattern between " ", if it doesn't matches it returns true. It is a condition which checks for a match to the regular expression given in pattern.

The preg_match can be called as:

preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

In your case the $pattern is "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i" and the $subject / the input string is $website. Which seems to be checking for https and ftp.

You can enter your pattern string in a website like this one and hover over each element for a clue as to what it means.

Laurentiu L.
  • 6,213
  • 1
  • 28
  • 55
  • thank you.....but I am confused in using '/\ ' and '\/\/' and [-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i" – ArjunKasi Sep 07 '15 at 04:59
  • The pattern is enclosed by forward slash / pattern delimiters , next comes the word boundary \b which is used for matching the start position in this case, next comes the parentheses () which are used to define the scope and precedence of the operators. ?: is used for a non-capturing group (?:pattern) so it's not just /\ it's / .... / and \b. More than that you just need to learn regex to understand it all. The website i provided should only validate the input. Good luck: http://regexone.com/ – Laurentiu L. Sep 07 '15 at 07:26