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
https://forums.phpfreaks.com/topic/208461-turn-url-to-link/
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
https://forums.phpfreaks.com/topic/208461-turn-url-to-link/#findComment-1089769
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
https://forums.phpfreaks.com/topic/208461-turn-url-to-link/#findComment-1089771
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
https://forums.phpfreaks.com/topic/208461-turn-url-to-link/#findComment-1089778
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.