Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jan 2006
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    convert http:// to [link]

    So i have a chat, and currently when you type in a link(http://codingforums.com), that is all it would put, it doesnt put in a link. Thats normal seeing as i havent made a function for making it do that yet. I do however have a script that converts smilies, and some BBcode. I was wondering if i could use that same script to make it so if something had http:// in the front of it, it would make that a link. My current scripts are as follows:

    PHP Code:
    <?
    $search 
    = array(':)',':(',':s',':S',':x',':X',':p',':P',':o',':O',':lol:',':cry:',':$',':roll:',':evil:',':twisted:',':!:',':?:',':D',':d','8)',':idea:',':arrow:',';)','8o','[b]','[/b]','[u]','[/u]','[i]','[/i]','[code]','[/code]','/whois ','/admin ','/c mods ','/c newbie ',

    );



    $replace = array(
    " <img src=\"./img/smile.gif\"> ",
    " <img src=\"./img/sad.gif\"> ",
    " <img src=\"./img/confused.gif\"> "," <img src=\"./img/confused.gif\"> ",
    " <img src=\"./img/mad.gif\"> "," <img src=\"./img/mad.gif\"> ",
    " <img src=\"./img/tounge.gif\"> "," <img src=\"./img/tounge.gif\"> ",
    " <img src=\"./img/surprised.gif\"> "," <img src=\"./img/surprised.gif\"> ",
    " <img src=\"./img/lol.gif\"> ",
    " <img src=\"./img/cry.gif\"> ",
    " <img src=\"./img/embarrassed.gif\"> ",
    " <img src=\"./img/rolleyes.gif\"> ",
    " <img src=\"./img/evil.gif\"> ",
    " <img src=\"./img/twisted.gif\"> ",
    " <img src=\"./img/exclaim.gif\"> ",
    " <img src=\"./img/question.gif\"> ",
    " <img src=\"./img/happy.gif\"> ",
    " <img src=\"./img/happy.gif\"> "," <img src=\"./img/cool.gif\"> ",
    " <img src=\"./img/idea.gif\"> ",
    " <img src=\"./img/arrow.gif\"> ",
    " <img src=\"./img/wink.gif\"> ",
    " <img src=\"./img/shocked.gif\"> ",
    " <b> "," </b> "," <u> "," </u> "," <i> "," </i> ",
    " <pre><font size=\"3\"> "," </font></pre> ","<a target=\"centerarea\" href=\"map.php?viewx=".$retval5['xpos']."&viewy=".$retval5['ypos']."\">","<a target=\"centerarea\" href=\"admin.php\">" ,"<a href=\"shout_mods.php?channel=mods\">","<a href=\"shout_newbie.php?channel=newbie\">",

    );




    $babble2 strip_tags($message);
    $babble str_replace($search$replace$babble2);
    ?>
    any help would be greatly appreciated. Thanks!

  • #2
    Regular Coder mic2100's Avatar
    Join Date
    Feb 2006
    Location
    Scunthorpe
    Posts
    562
    Thanks
    15
    Thanked 28 Times in 27 Posts
    hi,

    i have been playing around a bit and have come up with this...

    PHP Code:

    <?

    //just an example
    $string "hi this my home page http://www.mike.com and the link you wanted http://www.text.com";

    //this extracts all the url pieces and place them in an array
    preg_match_all("^((ht|f)tp(s?)\:\/\/|~/|/)?([\w]+:\w+@)?([a-zA-Z]{1}([\w\-]+\.)+([\w]{2,5}))(:[\d]{1,5})?((/?\w+/)+|/?)(\w+\.[\w]{3,4})?((\?\w+=\w+)?(&\w+=\w+)*)?^"$string$array);

    //if there is anything in the array then it must be processed
    if(sizeof($array) > 0):

        foreach(
    $array as $keys => $value):
            
            
    //print_r($value);
        
            
    foreach($value as $key => $url):
            
            
    //if the first 7 chars are http:// and the length of the string is over 7 chars
                
    if(substr($url07) == "http://" and strlen($url) > 7):
            
                    
    //a link is created    
                    
    $string str_replace($url"<a href='".$url."' target='_blank'>".$url."</a>"$string);
                
                endif;
            
            endforeach;
        
        endforeach;
        
        echo 
    $string;

    else:

        
    //no links detected

    endif;

    ?>
    i hope this is what you are after and good luck with it.

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    PHP Code:
    $subject 'short message including a url https://www.codingforums.com';
    $subject preg_replace('#(https?://[^ ]+)#s''<a href="\\1">\\1</a>'$subject);
    /* outputs:

    short message including a url <a href="https://www.codingforums.com">https://www.codingforums.com</a>
    */ 
    You could make the regex more complicated, but this should work/get you started.
    Last edited by Inigoesdr; 02-24-2008 at 09:45 PM.

  • #4
    Regular Coder
    Join Date
    Jan 2006
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks! that did work! i modified it to:

    PHP Code:
    $babble preg_replace('#(http?://[^ ]+)#s''<a target=\"blank\" href="\\1"><font color=\"0000FF\">[link]</font></a> <font color=\"000000\">\\1'$babble); 
    essentially the same thing. Thanks a lot

  • #5
    Regular Coder mic2100's Avatar
    Join Date
    Feb 2006
    Location
    Scunthorpe
    Posts
    562
    Thanks
    15
    Thanked 28 Times in 27 Posts
    thanks also inigo i knew there had to be a shorter way


  •  

    Posting Permissions

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