Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-11-2013, 12:44 AM   PM User | #1
Dubz
Regular Coder

 
Join Date: Sep 2011
Posts: 206
Thanks: 15
Thanked 5 Times in 5 Posts
Dubz has a little shameless behaviour in the past
Question Get a url from a string

I would like to be able to find the url(s) in the given string. It does not always have http:// before it and it's not always a .com domain, so it's a little tricky. I don't care if this find things such as ASP.net or 5.6 because it's how it's going to work.

So basically, i need a way to strip out strings that have a period in them with no space before or after the period. For example, the following string would output the following (possible) url's

google.com offers a variety of ways to discover new websites. You can learn about asp.net and .net framework, depending on your search.

According to the site this script corresponds with, it would return "google.com", "asp.net" and ".net" as a possible url, so apparently if a period has a letter after it, it's a url.
Dubz is offline   Reply With Quote
Old 01-11-2013, 11:46 AM   PM User | #2
hinch
Regular Coder

 
hinch's Avatar
 
Join Date: Sep 2005
Location: UK
Posts: 921
Thanks: 25
Thanked 79 Times in 79 Posts
hinch is on a distinguished road
regex for fqdn. something like this may/may not be 100% accurate or working off top of head without any form of testing

PHP Code:
$txt='google.com offers a variety of ways to discover new websites. You can learn about asp.net and .net framework, depending on your search.';

  
$re1='((?:[a-z][a-z\\.\\d\\-]+)\\.(?:[a-z][a-z\\-]+))(?![\\w\\.])';    # Fully Qualified 
  
$re2='.*?';    # Non-greedy match on filler

  
if ($c=preg_match_all ("/".$re1.$re2."/is"$txt$matches))
  {
      for (
$i=0;$i<count($matches);$i++) {
         echo 
$matches[$i][0]."<br />";
      }
  } 
__________________
A programmer is just a tool which converts caffeine into code

My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
My hobby: http://www.angel-computers.co.uk
My life: http://www.furious-angels.com
hinch is offline   Reply With Quote
Old 01-11-2013, 09:15 PM   PM User | #3
Dubz
Regular Coder

 
Join Date: Sep 2011
Posts: 206
Thanks: 15
Thanked 5 Times in 5 Posts
Dubz has a little shameless behaviour in the past
I tried executing that hinch, all it returned was this:

Code:
google.com<br />google.com<br />
EDIT:
I remember coming across this online Regex test tool, might be useful for any future needs.

http://www.spaweditor.com/scripts/regex/index.php
Dubz is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:06 PM.


Advertisement
Log in to turn off these ads.