Results 1 to 5 of 5

Thread: Text URL to hyperlink (ignore existing <img> and <a> tags)

  1. #1
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default Text URL to hyperlink (ignore existing <img> and <a> tags)

    I've been using this function to convert URLs from a text file to clickable links, but it also converts the URL in existing <img> and <a> tags too.

    Code:
    function makeClickableLinks($text) {
      $text = eregi_replace('(((f|ht){1}tp://)[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '<a href="\\1">\\1</a>', $text);
      $text = eregi_replace('([[:space:]()[{}])(www.[-a-zA-Z0-9@:%_\+.~#?&//=]+)', '\\1<a href="http://\\2">\\2</a>', $text);
      $text = eregi_replace('([_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3})', '<a href="mailto:\\1">\\1</a>', $text); 
    return $text;
    }
    I'm not fully understanding patterns in PHP yet so please could somebody help me convert the above function to ignore the URL if its already inside an <img> or <a> tag and only convert the URL if its floating loose in the text.

    If it helps aswell, the existing image tags always look something like this (no alt, height, width, style, class or id);
    <img src="http://www.website.com/mypic.jpg">

    And the existing anchors always look something like this (no title, style, class or id);
    <a href="http://www.website.com/page.html">Some Text</a>

    Fixed values are in blue. Both may or may not have the www. in the URL.

    Hope that's enough info
    Last edited by Beverleyh; 04-21-2011 at 10:14 AM. Reason: URL info addedd
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    I'm still curious to find a fix for the function above but I just wanted to let you know that I've found another way that suits my particular setup - my particular setup being that the URL needing to be made clickable is always on its own line, and has the text prefix 'Visit this link:'. So using that as an identifier, I can use the method below;

    Code:
    $handle = fopen('/path/to/file.txt', 'r');
    $identifier = 'Visit this link:'; 
    if ($handle) {
    	while (!feof($handle)) {
    		$text = fgets($handle);
    		if (is_integer(strpos($text,$identifier))) { // find lines with 'Visit this link:' with text URL to be made clickable
    		echo makeClickableLinks($text); // only apply makeClickableLinks() function to lines where identifier is found
    		} else {
    		echo $text;
    		}
    	}
    fclose($handle);
    }
    Last edited by Beverleyh; 04-21-2011 at 11:58 AM. Reason: correction to code
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Code:
    $text=
        "test 1: <a href=\"http://www.animeviews.com/article.php?ID=59\">http://www.animeviews.com/article.php?ID=59</a>. word.
     <br>test 2: www.animeviews.com/article.php?ID=59. word.
     <br>test 3: http://www.animeviews.com/article.php?ID=59. word.
     <br>test 4: <a href=\"http://www.animeviews.com\">test 4</a>. word.
     <br>test 5: <a href=\"www.animeviews.com\">www.animeviews.com</a> word.
     <br>test 6: http://en.wikipedia.org/wiki/Portable_Network_Graphics word.";
    Code:
    <?php
    $text = preg_replace('/(?<!http:\/\/|https:\/\/|\"|=|\'|\'>|\">)(www\..*?)(\s|\Z|\.\Z|\.\s|\<|\>|,)/i',"<a href=\"http://$1\">$1</a>$2",$text);
    $text = preg_replace('/(?<!\"|=|\'|\'>|\">|site:)(https?:\/\/(www){0,1}.*?)(\s|\Z|\.\Z|\.\s|\<|\>|,)/i',"<a href=\"$1\">$1</a>$3",$text);
    echo "$text";
     ?>
    I have a small collection of PCRE. The one above is what I use for what you are describing. It has served me very well. Test 5, listed above is broken, but this is by design. It is not designed to fix broken hyperlinks, only to correctly hyperlink web addresses.
    Last edited by james438; 04-22-2011 at 01:41 AM. Reason: minor correction
    To choose the lesser of two evils is still to choose evil. My personal site

  4. The Following User Says Thank You to james438 For This Useful Post:

    Beverleyh (04-22-2011)

  5. #4
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    3,033
    Thanks
    25
    Thanked 599 Times in 575 Posts
    Blog Entries
    40

    Default

    That's very helpful James, thank you
    Focus on Function Web Design
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps

  6. #5
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    no problem. Glad I could help
    To choose the lesser of two evils is still to choose evil. My personal site

Bookmarks

Posting Permissions

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