Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

match website address not working

Posted on 2007-07-26
7
Medium Priority
?
209 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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 …
Suggested Courses

926 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