Solved

match website address not working

Posted on 2007-07-26
7
204 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:Richard Quadling
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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:Richard Quadling
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

685 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