Jump to content

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.