Jump to content

[SOLVED] Making all URLs in a string into hyperlinks


avvllvva

Recommended Posts

Hi,

 

Here I have a plain-text string, it may contain one or more URLs inside it. I want to make all of them into hyperlinks.

 

This Example explain more :-

<?php
$str = " Plain text plain text plain textplain textplain textplain text plain text  plain text plain text plain text.  Plain text plain text plain text
	 plain text plain text plain text plain www.google.com/analytics text plain text plain text. Plain text  plain text plain text plain text plain text 
         plain text plain text
         plain text plain text plain text http://yahoo.com plain text plain text plain text plain text plain text plain text plain text plain text plain text
         plain text plain text.  Plain text  plain text plain text plain text https://www.example.com plain text plain text plain text plain text.
	"; 
?>

 

In the above string you can see 3 URLs, say

1. www.google.com/analytics

2. http://yahoo.com

3. https://www.example.com

(I think, i've covered all possible formats of an URL in the above three.)

 

 

So whenever echoing this string I want to make these URLs into hyperlinks. ie; appending anchor tag at the begining.

 

Output like this:

----------------------------------------------------------------------------------------------

Plain text plain text plain textplain textplain textplain text plain text  plain text plain text plain text.  Plain text plain text plain text

plain text plain text plain text plain  www.google.com/analytics  text plain text plain text. Plain text  plain text plain text plain text plain text

        plain text plain text

        plain text plain text plain text  http://yahoo.com  plain text plain text plain text plain text plain text plain text plain text plain text plain text

        plain text plain text.  Plain text  plain text plain text plain text https://www.example.com plain text plain text plain text plain text.

----------------------------------------------------------------------------------------------

 

Can anybody help me to acheving this?

 

Link to comment
Share on other sites

$str = " Plain text plain text plain textplain textplain textplain text plain text  plain text plain text plain text.  Plain text plain text plain text
       plain text plain text plain text plain www.google.com/analytics text plain text plain text. Plain text  plain text plain text plain text plain text 
         plain text plain text
         plain text plain text plain text http://yahoo.com plain text plain text plain text plain text plain text plain text plain text plain text plain text
         plain text plain text.  Plain text  plain text plain text plain text https://www.example.com plain text plain text plain text plain text.
      "; 

preg_match_all('~http://(.*?).com~is', $str, $matches);

foreach ($matches[1] as $link) {

echo "<a href = 'http://{$link}.com'>{$link}</a><br>";
}

Link to comment
Share on other sites

Thank you for your help.

 

It outputs only a word "yahoo" but it is hyper linked.

ie;

----------------------------------------------------------------------------------------------

yahoo

----------------------------------------------------------------------------------------------

but i'm looking for this

----------------------------------------------------------------------------------------------

Plain text plain text plain textplain textplain textplain text plain text  plain text plain text plain text.  Plain text plain text plain text

      plain text plain text plain text plain  www.google.com/analytics  text plain text plain text. Plain text  plain text plain text plain text plain text

        plain text plain text

        plain text plain text plain text  http://yahoo.com  plain text plain text plain text plain text plain text plain text plain text plain text plain text

        plain text plain text.  Plain text  plain text plain text plain text https://www.example.com plain text plain text plain text plain text.

----------------------------------------------------------------------------------------------

 

Link to comment
Share on other sites

Finally I got the solution from php.net, here is the one

<?php
function addLinks($string) {
$string = preg_replace("/(^|[^=\"\/])\b((\w+:\/\/|www\.)[^\s<]+)".
    					"((\W+|\b)([\s<]|$))/i", "$1<a href=\"$2\" target='_blank'>$2</a>$4", $string);
return    preg_replace("/href=\"www/i", "href=\"http://www", $string);
}
echo addLinks($str);

?>

Regular expression did the job, anyway I have to study more on that. ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.