Jump to content

Turn url to link


EchoFool

Recommended Posts

Hey,

 

I am trying to use my function replace urls into <A href=""> in the array but do not know how i would do it with the current function i am using for my BBCode....

 

Say a user types www.google.com i want it to be clickable in the post.

 

My BBCode function is:

 

<?php
//bb code fucntion
function BBCodechat($BB){
                    $BBCode = array("&" => "&",
                        "[b]" => "<b>",
                        "[/b]" => "</b>",
                        "[i]" => "<i>",
                        "[/i]" => "</i>",
                        "[u]" => "<u>",
                        "[/u]" => "</u>"                        
                        );
                    
                    $Message = str_ireplace(array_keys($BBCode), array_values($BBCode), $BB);
                return $Message;
                                }
?>

 

Hope you can help.

Link to comment
Share on other sites

This one in particular looked fitting to me.

 

<?php
function change_string($str)
   {
     $str = trim($str);
     $str = htmlspecialchars($str);
     $str = preg_replace('#(.*)\@(.*)\.(.*)#','<a href="mailto:\\1@\\2.\\3">Send email</a>',$str);
     $str = preg_replace('=([^\s]*)(www.)([^\s]*)=','<a href="http://\\2\\3" target=\'_new\'>\\2\\3</a>',$str);
     return $str;
   }
?>

Link to comment
Share on other sites

You mean this?

 


<?php
function BBCodechat($BB)
   {
     $str = trim($BB);
     $str = htmlspecialchars($BB);
     $str = preg_replace('#(.*)\@(.*)\.(.*)#','<a href="mailto:\\1@\\2.\\3">Send email</a>',$BB);
     $str = preg_replace('=([^\s]*)(www.)([^\s]*)=','<a href="http://\\2\\3" target=\'_new\'>\\2\\3</a>',$BB);
     return $BB;
   }
?>

Link to comment
Share on other sites

Do this then:

 

function BBCodechat($BB){
    $BBCode = array("&" => "&",
        "[b]" => "<b>",
        "[/b]" => "</b>",
        "[i]" => "<i>",
        "[/i]" => "</i>",
        "[u]" => "<u>",
        "[/u]" => "</u>"                        
    );
    $Message = str_ireplace(array_keys($BBCode), array_values($BBCode), $BB);
    $Message = trim($Message);
    $Message = htmlspecialchars($Message);
    $Message = preg_replace('#(.*)\@(.*)\.(.*)#','<a href="mailto:\\1@\\2.\\3">Send email</a>',$Message);
    $Message = preg_replace('=([^\s]*)(www.)([^\s]*)=','<a href="http://\\2\\3" target=\'_new\'>\\2\\3</a>',$Message);
    return $Message;
}

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.