Solved

match website address not working

Posted on 2007-07-26
7
203 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

838 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