HMBeaty Posted September 25, 2011 Share Posted September 25, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/247837-breaking-down-translating-this-php-code/ Share on other sites More sharing options...
jcbones Posted September 25, 2011 Share Posted September 25, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/247837-breaking-down-translating-this-php-code/#findComment-1272659 Share on other sites More sharing options...
HMBeaty Posted September 25, 2011 Author Share Posted September 25, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/247837-breaking-down-translating-this-php-code/#findComment-1272660 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.