Jump to content

Breaking down / translating this php code


HMBeaty

Recommended Posts

Hoping someone can help me on this, but I had some help on getting this code (or part of it) to finally work the way I wanted it, however, there are a few end-user issues with it now.

 

What this code does is disables the user(s) from posting any kind of links (www.example.com or example.com). However, now I'm trying to....expand it a little I guess.

 

Here is the current code:

if (stristr($pagetext, 'http://') OR stristr($pagetext, 'www.') OR stristr($pagetext,'@') OR stristr($pagetext, '[url') OR stristr($pagetext, '[url') OR stristr($pagetext, '[img') OR stristr($pagetext, '[img') OR preg_match("#[a-z0-9]([-a-z0-9]+)?(\.[a-z]{2,3})?(\.[a-z]{2,4})#i", $pagetext))
            {
                more code here
            }

 

Now, I KNOW what most of that code does, but what I need, is what exactly this code does:

preg_match("#[a-z0-9]([-a-z0-9]+)?(\.[a-z]{2,3})?(\.[a-z]{2,4})#i", $pagetext)

Can anyone break this down and tell me what exactly does what?

 

Thanks in advance :)

Link to comment
https://forums.phpfreaks.com/topic/247837-breaking-down-translating-this-php-code/
Share on other sites

In short, it gets a url that doesn't have a http:// or a www. in front of it. 

 

Valid:

9however.com

starthere.org

tomorrow.ca.org

a.us.gov

 

Explain:

[a-z0-9]([-a-z0-9]+)?(\.[a-z]{2,3})?(\.[a-z]{2,4}) //pattern

[a-z0-9] //pattern starts with a letter, or a number
([-a-z0-9]+)? //capture group 1 second character must be a letter or a number, must be at least one character to capture.  (optional parameter '?', which means you don't have to capture this group).
(\.[a-z]{2,3})? //capture group 2 starts with a . (dot) followed by 2 to 3 letters (optional, doesn't have to exist).
(\.[a-z]{2,4}) //capture group 3 starts with a . (dot) followed by 2 to 4 letters.  

# = delimiter, as the pattern rest inside of them.
i -> after the closing delimiter means it is a case-insensitive match.

 

RegEx is powerful, but difficult to get your mind wrapped around.  Great thing to learn though.

In short, it gets a url that doesn't have a http:// or a www. in front of it. 

 

Valid:

9however.com

starthere.org

tomorrow.ca.org

a.us.gov

 

Explain:

[a-z0-9]([-a-z0-9]+)?(\.[a-z]{2,3})?(\.[a-z]{2,4}) //pattern

[a-z0-9] //pattern starts with a letter, or a number
([-a-z0-9]+)? //capture group 1 second character must be a letter or a number, must be at least one character to capture.  (optional parameter '?', which means you don't have to capture this group).
(\.[a-z]{2,3})? //capture group 2 starts with a . (dot) followed by 2 to 3 letters (optional, doesn't have to exist).
(\.[a-z]{2,4}) //capture group 3 starts with a . (dot) followed by 2 to 4 letters.  

# = delimiter, as the pattern rest inside of them.
i -> after the closing delimiter means it is a case-insensitive match.

 

RegEx is powerful, but difficult to get your mind wrapped around.  Great thing to learn though.

Awesome! Thank you!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.