Goldeneye Posted August 23, 2009 Share Posted August 23, 2009 So, this has been on my project To-Do List for a long time. I'm trying to implement a [/code] into my BB-Code function. I use square-bracket tags (such as [/b], [/u], and [/img]). No HTML or JavaScript is parsed, but square-bracket tags are. My goal is to not have the square-bracket tags parsed inside the [/code] tag. <?php function formatText($str) { $str = trim($str); $str = stripslashes($str); $str = htmlentities($str, ENT_COMPAT, 'UTF-8'); $str = nl2br($str); $search = array('/\[b\](.*?)\[\/b\]/si', '/\[i\](.*?)\[\/i\]/si', '/\[u\](.*?)\[\/u\]/si', '/\[align\=(left|center|right)\](.*?)\[\/align\]/si', '/\[img\](.*?)\[\/img\]/si', '/\%n/si', '/\[priv\](.*?)\[\/priv\]/si', '/\[ascii\](.*?)\[\/ascii\]/si', '/((mailto:|(http|ftp|nntp|news):\/\/).*?)(\s|<|\)|"|\\\\|\'|$)/', '/\[hide\](.*?)\[\/hide\]/si'); $replace = array('<b>$1</b>', '<i>$1</i>', '<u>$1</u>', '<div style="text-align: $1;">$2</div>', '<a href="$1"><img src="$1" class="image"></a>', $_SESSION['pseudonym'], isset($_SESSION['user_id']) ? '$1' : '<p class="sysmessage">For members\' eyes only.</p>', '<span class="ascii">$1</span>', '<a href="$1" rel="nofollow" target="_blank">$1</a>$4', '<span class="view_closed"><span class="view_on_close"><a href="#" onclick="return toggleContent(this)"><strong>[view]</strong></a></span><span class="view_on_open"><a href="#" onclick="return toggleContent(this)">[hide]</a>$1</span></span>'); preg_match_all('/\[code\](.*?)\[\/code\]/si', $str, $code); foreach($code[1] as $stri) $str = str_replace('[', '&92;', $stri); $str = preg_replace($search, $replace, $str); return $str; } ?> The replace works fine, but it shows the entity as opposed to it's special-character form. So, this: <?php $str = formatText('[code][b]This[/b] is [i]italicized[/i] [u]text[/u][/ code]'); echo $str; ?> Results in this: &92;b]This&92;/b] is &92;i]italicized&92;/i] &92;u]text&92;/u] Is there a way to actually get this to work? Or is there a completely different way to do this that will ensure its functionality? A preemptive thanks for reading. Link to comment https://forums.phpfreaks.com/topic/171463-solved-implementing-a-properly-functioning-code-tag/ Share on other sites More sharing options...
Goldeneye Posted August 23, 2009 Author Share Posted August 23, 2009 Well this is embarassing, it turns out the code works perfectly. I was just using an invalid HTML-Entity. &92; is not the character-entity for a left-square bracket - [ is. Link to comment https://forums.phpfreaks.com/topic/171463-solved-implementing-a-properly-functioning-code-tag/#findComment-904301 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.