Solved

match website address not working

Posted on 2007-07-26
7
201 Views
Last Modified: 2008-02-20
ive got this snippet of code to match a website address and create a link from it.

however the link it creates contains the rest of the string after the TLD part.

example:

$data = '<p>These are some of my previous projects:</p><ul><li>www.antons-law.com</li><li>www.lisafinlaymakeupartist.com</li><li>www.quest-recruiting.com</li></ul><p>What do you think?</p>';

the output would be:

$data = '<p>These are some of my previous projects:</p><ul><li><a onclick="javascript:window.open(this.href);return false;" rel="nofollow" href="www.antons-law.com</li><li>www.lisafinlaymakeupartist.com</li><li>www.quest-recruiting.com</li></ul><p>What" title="">www.antons-law.com</li><li>www.lisafinlaymakeupartist.com</li><li>www.quest-recruiting.com</li></ul><p>What</a> do you think?</p>';

see what i mean?  how can i fix this??  here is my code:

<?php
function fcnConvertWebsiteAddress($data)
{
      $data = preg_replace('/(https?\:\/\/\S*)/','<a href="${1}" title="">${1}</a>',$data);
      return preg_replace('/(?<![\'"])([^\/])(www\.[^\s\,"]+)/','$1<a href="${2}" title="">${2}</a>',$data);
}

$data = '<p>These are some of my previous projects:</p><ul><li>www.antons-law.com</li><li>www.lisafinlaymakeupartist.com</li><li>www.quest-recruiting.com</li></ul><p>What do you think?</p>';

$data = fcnConvertWebsiteAddress($data);

echo nl2br($description);
?>

the reason for the 2 preg_replaces is that i want to match all possible types of website addresses...

Ellandrd
0
Comment
Question by:ellandrd
  • 4
  • 2
7 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 19572600
Use as second regexp:
return preg_replace('/(?<![\'"])([^\/])(www\.[^\s\,"<]+)/','$1<a href="${2}" title="">${2}</a>',$data);
note the additional < in .[^\s\,"<]
0
 
LVL 16

Author Comment

by:ellandrd
ID: 19572625
Works Perfect!

Thank you

Ellandrd
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 19572635
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_22719226.html

Hmmm. I hope you don't have 2 accounts!!!!

Or are we doing someone's homework..

Almost identical questions.

Hmmmm......
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 16

Author Comment

by:ellandrd
ID: 19572649
Richard why on earth would i want 2 accounts?

the question isn't homework either and ive being working on this snippet for a few weeks now.  hernst42 can back that up as he has helped me with alot of it.

Ellandrd
0
 
LVL 16

Author Comment

by:ellandrd
ID: 19572656
and just to prove my point - here is one of my previous questions that you answered for me that relates to this snippet of code:

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_22691975.html

Ellandrd
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 19572660
He he! Only joking. I know! Just that I answered a very similar question yesterday.
0
 
LVL 16

Author Comment

by:ellandrd
ID: 19572695
...and if you require a list of my previous questions regarding the complete working of my application i can provide that too...

i think the asker in the previous question you posted above was trying to do something similar to me.

Ellandrd
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now