Jump to content

simple preg_match help (i think)


TAz69x

Recommended Posts

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!

Link to comment
Share on other sites

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.