3

What's a regex pattern (in PHP) that replaces a string with hyperlinks, where the text preceding a URL is used as the anchor text for the link? For example:

text a http://example.com ending text

becomes

<a href="http://example.com">text a</a> ending text

In other words, the text preceding the URL becomes the anchor text for the link.

What I really want is a variation of the following function by snipe http://www.snipe.net/2009/09/php-twitter-clickable-links/ but with the twist above.

  • Pardon my ignorance: what is "snipe's function"? Google doesn't come up with anything relevant. –  Feb 17 '11 at 01:41
  • Well i would have hyperlinked to it but didn't have enough karma here do put in another link. Snipe has a great function that hyperlinks text in raw twitter text ... just what i need ... but withouth the twist i want above. You can find his function here http://www.snipe.net/2009/09/php-twitter-clickable-links/ – Seth Russell Feb 17 '11 at 01:49
  • To clarify, you want the anchor text to match the text that immediately *follows* the hyperlink. Correct? Your example indicates this, but your problem description asks for the text "preceding the hyperlink". –  Feb 17 '11 at 02:22
  • I want the text that immediately **preceding** the link to become the anchor text of the link. Sorry i had a typo in my original question which has been corrected now. – Seth Russell Feb 17 '11 at 15:48
  • Have you tried to search before posting your question ? – Pedro Lobito Apr 26 '15 at 20:16

1 Answers1

3

Here's a modified version of snipe's twitterify():

<?php
function twitterify($ret) {
  //
  // Replace all text that precedes a URL with an HTML anchor
  // that hyperlinks the URL and shows the preceding text as
  // the anchor text.
  // 
  // e.g., "hello world www.test.com" becomes
  // <a href="www.test.com" target="_blank">hello world</a>
  //
  $ret = preg_replace("#(.*?)(http://)?(www\.[^ \"\t\n\r<]+)#", "<a href=\"http://\\3\" target=\"_blank\">\\1</a>", $ret);

  // if anchor text is empty, insert anchor's href
  $ret = preg_replace("#(<a href=\"(\w+://)?([^\"]+)\"[^>]+>)(</a>)#", "\\1\\3\\4", $ret);

  $ret = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $ret);
  $ret = preg_replace("/#(\w+)/", "<a href=\"http://search.twitter.com/search?q=\\1\" target=\"_blank\">#\\1</a>", $ret);
  return $ret;
}

Testing the code above with test()...

function test($str) {
  print "INPUT:  \"" . $str . "\"\nOUTPUT: " . twitterify($str) . "\n\n";
}
// tests
test("www.foo.com");
test("www.foo.com  fox");
test("www.test.com  fox jumped over  www.foo.com");
test("fox jumped over  www.test.com   the fence   www.foo.com");
?>

...results in the following print-outs.

INPUT:  "www.foo.com"
OUTPUT: <a href="http://www.foo.com" target="_blank">www.foo.com</a>

INPUT:  "www.foo.com  fox"
OUTPUT: <a href="http://www.foo.com" target="_blank">www.foo.com</a>  fox

INPUT:  "www.test.com  fox jumped over  www.foo.com"
OUTPUT: <a href="http://www.test.com" target="_blank">www.test.com</a><a href="http://www.foo.com" target="_blank">  fox jumped over  </a>

INPUT:  "fox jumped over  www.test.com   the fence   www.foo.com"
OUTPUT: <a href="http://www.test.com" target="_blank">fox jumped over  </a><a href="http://www.foo.com" target="_blank">   the fence   </a>

Tested on ideone.

EDIT: Updated code to match new requirements

  • No it's not just a single word, it is all the text up to the previous previous link. That text becomes the anchor and goes away - see my new version of the question which i edited now that i understand how things work here better. This "new twist" is supposed to be the fastest, easiest to learn, way to get hyperlinks into text without showing them explicitly. A variation on it is where you use | to clear the text buffer. – Seth Russell Feb 17 '11 at 16:01
  • what i don't know how to do is the part of the expression that gets any text whatsoever that is **not** a link ... then i would put that in front of the expression for the link to designate that it goes in the anchor. of course that needs to repeat for as many times as it happens in the string. I really don't have the knack of regex yet :( – Seth Russell Feb 17 '11 at 16:27
  • This quesion has not been answered yet. I still need the expression that will select **all the text** since the last link and put it in the anchor as per the revised question above. – Seth Russell Feb 18 '11 at 00:54
  • My updated answer does the trick. –  Feb 18 '11 at 02:33
  • 's answer works only for URLs that start with www. I would like a solution that works for all http:// URLs or where someone just writes www.URL and omits the http:// – Seth Russell Feb 18 '11 at 14:32