lszanto Posted May 4, 2007 Share Posted May 4, 2007 I'm working on a news system and I've got the bbcode working but I'm working on a function to turn html into bbcode so they can edit news, this is how far I've got and everything works apart from the <img src="imagelink" /> to bit so please help. function unbbcode($post) { //Make the vars. $b = "/\<b\>(.*?)\<\/b\>/is"; $i = "/\<i\>(.*?)\<\/i\>/is"; $u = "/\<u\>(.*?)\<\/u\>/is"; $url = "/\<a href=(.*?)\ target=\"_blank\"\>(.*?)\<\/a\>/is"; $img = "/\<img src=(.*?)\ alt=\"image\" \>/is"; //Run preg_replace. $post = preg_replace($b, "[b]$1[/b]", $post); $post = preg_replace($i, "[i]$1[/i]", $post); $post = preg_replace($u, "[u]$1[/u]", $post); $post = preg_replace($url, "[url=$1]$2[/url]", $post); $post = preg_replace($img, "[img=$1]", $post); //Do str_replace loop for smilies. for($i=0; $i < 23; $i++) { $post = str_replace("<img src=\"images/smilies/$i.gif\" alt=\"Smiley$i\" />", ":$i:", $post); } //Return results. return $post; } Link to comment https://forums.phpfreaks.com/topic/49947-solved-bbcode-help/ Share on other sites More sharing options...
bob_the _builder Posted May 4, 2007 Share Posted May 4, 2007 Hi, I use something like: $BBCode = array( "<center>" => " [center]", "</center>" => "[/center] ", "<b>" => "[b]", "</b>" => "[/b]", "<u>" => "[u]", "</u>" => "[/u]", "<i>" => "[i]", "</i>" => "[/i]", "<img src=" => "[img=http://'", "'>" => "]", "<li>" => "[list][*]", "</li>" => "[/list]", "<font color='red'>" => "[color=red]", "</font>" => "[/color]", ); $value = str_replace(array_keys($BBCode), array_values($BBCode), $value); $value = mysql_real_escape_string(strip_tags(trim($value))); return $value; } Then reverse it for use with editing. hth Link to comment https://forums.phpfreaks.com/topic/49947-solved-bbcode-help/#findComment-245163 Share on other sites More sharing options...
lszanto Posted May 5, 2007 Author Share Posted May 5, 2007 I might try that but is there anyway to do it with preg_replace, I find it easier to use and it does the job better for me. Link to comment https://forums.phpfreaks.com/topic/49947-solved-bbcode-help/#findComment-245984 Share on other sites More sharing options...
neel_basu Posted May 5, 2007 Share Posted May 5, 2007 replace the [img] with <img src=" and[/img] to " /> Link to comment https://forums.phpfreaks.com/topic/49947-solved-bbcode-help/#findComment-245989 Share on other sites More sharing options...
neel_basu Posted May 5, 2007 Share Posted May 5, 2007 May be this can help You <?php function bbcode($string){ $string = nl2br(htmlspecialchars($string)); $patterns = array( '`\[b\](.+?)\[/b\]`is', '`\[i\](.+?)\[/i\]`is', '`\[u\](.+?)\[/u\]`is', '`\[strike\](.+?)\[/strike\]`is', '`\[color=#([0-9]{6})\](.+?)\[/color\]`is', '`\[email\](.+?)\[/email\]`is', '`\[img\](.+?)\[/img\]`is', '`\[url=([a-z0-9]+://)([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\](.*?)\[/url\]`si', '`\[url\]([a-z0-9]+?://){1}([\w\-]+\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*)?)\[/url\]`si', '`\[url\]((www|ftp)\.([\w\-]+\.)*[\w]+(:[0-9]+)?(/[^ \"\n\r\t<]*?)?)\[/url\]`si', '`\[flash=([0-9]+),([0-9]+)\](.+?)\[/flash\]`is', '`\[quote\](.+?)\[/quote\]`is', '`\[size=([1-6]+)\](.+?)\[/size\]`is' ); $replaces = array( '<strong>\\1</strong>', '<i>\\1</i>', '<u>\\1</u>', '<strike>\\1</strike>', '<span style="color:#\1;">\2</span>', '<a href="mailto:\1">\1</a>', '<img src="\1" alt="" style="border:0px;" />', '<a href="\1\2">\6</a>', '<a href="\1\2">\1\2</a>', '<a href="http://\1">\1</a>', '<object width="\1" height="\2"><param name="movie" value="\3" /><embed src="\3" width="\1" height="\2"></embed></object>', '<strong>Quote:</strong><div style="margin:0px 10px;padding:5px;background-color:#F7F7F7;border:1px dotted #CCCCCC;width:80%;"><em>\1</em></div>', '<h\1>\2</h\1>' ); $string = preg_replace($patterns, $replaces , $string); return $string; ?> Link to comment https://forums.phpfreaks.com/topic/49947-solved-bbcode-help/#findComment-245990 Share on other sites More sharing options...
lszanto Posted May 5, 2007 Author Share Posted May 5, 2007 replace the [img] with <img src=" and[/img] to " /> Thanks, that seemed to work for me. Link to comment https://forums.phpfreaks.com/topic/49947-solved-bbcode-help/#findComment-245997 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.