LemonInflux Posted November 3, 2007 Share Posted November 3, 2007 Well, it seems that I somehow killed the last page. So, I'm starting this one. I've been to the tutorial on the site, and it doesn't work (in fact, it outputs SMF-breaking text). Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/75926-parsing-help-bbcode-new-topic/ Share on other sites More sharing options...
LemonInflux Posted November 3, 2007 Author Share Posted November 3, 2007 If anyone wishes to try and get into the last topic, http://www.phpfreaks.com/forums/index.php/topic,166245.0.html Quote Link to comment https://forums.phpfreaks.com/topic/75926-parsing-help-bbcode-new-topic/#findComment-384319 Share on other sites More sharing options...
LemonInflux Posted November 3, 2007 Author Share Posted November 3, 2007 parse code: <?php class parser { var $parse_bbcodes; // Boolean - true = on | false = off var $parse_smilies; // Boolean - true = on | false = off var $smiley_url; // String - sets the URL to the smiley folder function parser() { // Set some default values: $this->parse_bbcodes = true; $this->parse_smilies = true; $this->smiley_url = "/smileys"; } function parse($data) { $data = htmlentities($data); if($this->parse_bbcodes) { $data = $this->_parse_bbcodes($data); } if($this->parse_smilies) { $data = $this->_parse_smilies($data); } $data = nl2br($data); return $data; } function _parse_bbcodes($data) { $data = preg_replace("`[(b|i|u|url|mail|img|center|right)][/(b|i|u|url|mail|img|center|right)]`",'',$data); $data = preg_replace("`[url](.*)[/url]`sUi","<a href='\1'>\1</a>",$data); $data = preg_replace("`[url=(.*)](.*)[/url]`sUi","<a href='\1'>\2</a>",$data); $data = preg_replace("`[mail](.*)[/mail]`sUi","<a href='mailto:\1'>\1</a>",$data); $data = preg_replace("`[mail=(.*)](.*)[/mail]`sUi","<a href='mailto:\1'>\2</a>",$data); $data = preg_replace("`[b](.*)[/b]`sUi","<span style='font-weight: bold;'>\1</span>",$data); $data = preg_replace("`[i](.*)[/i]`sUi","<span style='font-style: italic;'>\1</span>",$data); $data = preg_replace("`[u](.*)[/u]`sUi","<span style='text-decoration: underline;'>\1</span>",$data); $data = preg_replace("` [center](.*)[/center] `sUi","<div style='text-align: center;'>\1</div>",$data); $data = preg_replace("` [right](.*)[/right] `sUi","<div style='text-align: right;'>\1</div>",$data); $data = preg_replace("`[img=(.*)]`sUi","<img src='\1' alt='Posted Image' style='border: 0px;' />",$data); return $data; } function _parse_smilies($data) { $smilies = array( '' => 'smile.gif', '' => 'sad.gif', ); if(substr($this->smiley_url,-1) != '/' && !empty($this->smiley_url)) { $this->smiley_url .= '/'; } foreach($smilies as $code => $image) { $data = str_replace($code,"<img src='{$this->smiley_url}{$image}' alt='{$code}' />",$data); } return $data; } } ?> test code: <?php include 'bbcode.php'; $parser = new parser(); $default_text = <<<EOF This is a sample text. [url=http://phpfreaks.com]PHP Freaks[/url] is a PHP website [b]bold[/b] [b]bold again[/b] [i]italic[/i] [u]underline[/u] left [center]center[/center] [right]right[/right] [url]http://phpfreaks.com[/url] [mail=somebody@example.com]email[/mail] [mail]somebody@example.com[/mail] [center][url=http://google.com][img=http://www.google.com/intl/en/images/logo.gif][/url][/center] EOF; $text = !empty($_POST['text']) ? $_POST['text'] : $default_text; $ps_checked = (bool)$_POST['parse_smilies'] ? " checked='checked'" : null; $pbbc_checked = (bool)$_POST['parse_bbcodes'] ? " checked='checked'" : null; echo <<<EOF <form action='{$_SERVER['REQUEST_URI']}' method='post'> <label><input type='checkbox' name='parse_smilies' value='1'{$ps_checked}> Parse smilies</input></label><br /> <label><input type='checkbox' name='parse_bbcodes' value='1'{$pbbc_checked}> Parse BBcodes</input></label><br /><br /> <label>Text to parse:<br /><textarea cols='10' rows='15' style='width: 100%;' name='text'>{$text}</textarea></label><br /><br /> <button type='submit'>Parse text</button> </form> EOF; if(!empty($_POST['text'])) { $parser->parse_smilies = (bool) $_POST['parse_smilies']; $parser->parse_bbcodes = (bool) $_POST['parse_bbcodes']; $text = $parser->parse($_POST['text']); echo <<<EOF <hr /> <div style='overflow: auto; height: 200px;'>{$text}</div> EOF; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75926-parsing-help-bbcode-new-topic/#findComment-384349 Share on other sites More sharing options...
LemonInflux Posted November 3, 2007 Author Share Posted November 3, 2007 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/75926-parsing-help-bbcode-new-topic/#findComment-384375 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.