Solved

match website address not working

Posted on 2007-07-26
7
200 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
Comment Utility
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
Comment Utility
Works Perfect!

Thank you

Ellandrd
0
 
LVL 40

Expert Comment

by:RQuadling
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 16

Author Comment

by:ellandrd
Comment Utility
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
Comment Utility
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
Comment Utility
He he! Only joking. I know! Just that I answered a very similar question yesterday.
0
 
LVL 16

Author Comment

by:ellandrd
Comment Utility
...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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

12 Experts available now in Live!

Get 1:1 Help Now