Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    213
    Thanks
    15
    Thanked 5 Times in 5 Posts

    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.

  • #2
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    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

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    213
    Thanks
    15
    Thanked 5 Times in 5 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •