TAz69x Posted October 16, 2007 Share Posted October 16, 2007 I'm playing around with learning preg_match right now, and I want to match a website address entered into a textarea. This regex is far from anything concrete, just an end result of a mash of tests and changes made to play around with preg_match, though for now it serves a rudimentary purpose. preg_match('/.* +((www)+(\.)[[:alnum:]]+(\.)[[:alpha:]]{2,4}) +.*/i', stripslashes($_POST['test1']), $matches) (unrelated note, I match $matches[1] for the website) anyways, what I want is for, eg. if someone entered something like (each line is its own textarea submission): www.website.com blah www.website.com blah www.website.com blah that the website would be recognized (which I would later turn into an <a href ...> for example) but that something like: blahwww.website.comblah would NOT be recognized. then maybe in addition, something like blah www.website.comblah wouldn't be recognized either, because the last .comblah suffix is too long The main problem I'm having here is that, preferably without using a nested conditional, I'd like to test for an optional whitespace or space if there is one immediately prior to the www, and start the url from there, or if there is no whitespace or space before the www, to just start from the www instead. All the while keeping from something like ngraelkgrwww.website.comfdfjgkg being recognized. (which the . period does, because it represents any character) The main problem here is testing for the entire textarea for example, without using a nested conditional (if/elseif/elseif), to test for a space prior to the www. eg. www.website.com wouldn't be recognized with this current statement because it lacks a prior " " space (the ' +' metachar) www.website.com would be recognized though because of its prior space I'd want both to be recognizable in case the user decided to enter the url by itself, or after some text first for example. I guess I'm just looking for a magical character or syntax that says if there's a space before the www, start after it, or if there isn't, start right from the www. Then ultimately I'll want to be able to do this to several url entries in a single textarea (which is why I thought something like ^ and $ wouldn't be very useful), but for now, I'm wondering about this single issue first. Thanks so much for your help. PS - do you know of any REALLY good preg_match tutorials? They can perhaps start at fundamentals, it doesn't really matter, but then extend into classes and good techniques preferably. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/73441-simple-preg_match-help-i-think/ Share on other sites More sharing options...
effigy Posted October 16, 2007 Share Posted October 16, 2007 You can use word boundaries: \byour_pattern\b. Also, the [[:...:]] syntax is not supported in PREG. Quote Link to comment https://forums.phpfreaks.com/topic/73441-simple-preg_match-help-i-think/#findComment-370783 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.