...

View Full Version : convert http:// to [link]



king2k5
02-24-2008, 08:05 PM
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:



<?
$search = array(':)',':(',':s',':S',':x',':X',':p',':P',':o',':O',':lol:',':cry:',':$',':roll:',':evil:',':twi sted:',':!:',':?:',':D',':d','8)',':idea:',':arrow:',';)','8o','','','','','','','
','','/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!

mic2100
02-24-2008, 09:40 PM
hi,

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




<?

//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($url, 0, 7) == "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.

Inigoesdr
02-24-2008, 09:43 PM
$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.

king2k5
02-24-2008, 10:14 PM
Thanks! that did work! i modified it to:



$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 :)

mic2100
02-24-2008, 10:21 PM
thanks also inigo i knew there had to be a shorter way



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum