EchoFool Posted July 21, 2010 Share Posted July 21, 2010 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 More sharing options...
EchoFool Posted July 21, 2010 Author Share Posted July 21, 2010 bump Link to comment https://forums.phpfreaks.com/topic/208461-turn-url-to-link/#findComment-1089303 Share on other sites More sharing options...
EchoFool Posted July 22, 2010 Author Share Posted July 22, 2010 bump Link to comment https://forums.phpfreaks.com/topic/208461-turn-url-to-link/#findComment-1089760 Share on other sites More sharing options...
phpretard Posted July 22, 2010 Share Posted July 22, 2010 Have you looked through this? http://theserverpages.com/php/manual/en/function.preg-replace.php Link to comment https://forums.phpfreaks.com/topic/208461-turn-url-to-link/#findComment-1089764 Share on other sites More sharing options...
EchoFool Posted July 22, 2010 Author Share Posted July 22, 2010 woah that looks confusing, is my BBCode function not the best approach then Link to comment https://forums.phpfreaks.com/topic/208461-turn-url-to-link/#findComment-1089767 Share on other sites More sharing options...
phpretard Posted July 22, 2010 Share Posted July 22, 2010 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 More sharing options...
EchoFool Posted July 22, 2010 Author Share Posted July 22, 2010 Yeh but that i cannot include in my BBCode function can i cos its different functions...? Link to comment https://forums.phpfreaks.com/topic/208461-turn-url-to-link/#findComment-1089770 Share on other sites More sharing options...
phpretard Posted July 22, 2010 Share Posted July 22, 2010 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 More sharing options...
EchoFool Posted July 22, 2010 Author Share Posted July 22, 2010 No because then these: "&" => "&", "[b]" => "<b>", "[/b]" => "</b>", "[i]" => "<i>", "[/i]" => "</i>", "[u]" => "<u>", "[/u]" => "</u>" Won't be included ... Link to comment https://forums.phpfreaks.com/topic/208461-turn-url-to-link/#findComment-1089777 Share on other sites More sharing options...
mattal999 Posted July 22, 2010 Share Posted July 22, 2010 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.