Parsing bb code

Discussion in 'PHP' started by Tibiacity, Nov 19, 2008.

  1. #1
    Hello all,

    I was wondering how I can use php to parse lists like in the example below where the [*] isn't closed.


    [list]
    [*]
    [*]
    [/list]
     
    Tibiacity, Nov 19, 2008 IP
  2. !Unreal

    !Unreal Well-Known Member

    Messages:
    1,671
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    165
    #2
    !Unreal, Nov 19, 2008 IP
  3. Tibiacity

    Tibiacity Active Member

    Messages:
    190
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    73
    #3
    Yea but I can't really find a simple source for this code or something that explains how it actually works. Would like to learn from it :p.
     
    Tibiacity, Nov 20, 2008 IP
  4. !Unreal

    !Unreal Well-Known Member

    Messages:
    1,671
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    165
    #4
    This is how vBulletin parses it...

    Im not sure what you will make of it...

    
    /**
    	* Handles a [list] tag. Makes a bulleted or ordered list.
    	*
    	* @param	string	The body of the list.
    	* @param	string	If tag has option, the type of list (ordered, etc).
    	*
    	* @return	string	HTML representation of the tag.
    	*/
    	function handle_bbcode_list($text, $type = '')
    	{
    		if ($type)
    		{
    			switch ($type)
    			{
    				case 'A':
    					$listtype = 'upper-alpha';
    					break;
    				case 'a':
    					$listtype = 'lower-alpha';
    					break;
    				case 'I':
    					$listtype = 'upper-roman';
    					break;
    				case 'i':
    					$listtype = 'lower-roman';
    					break;
    				case '1': //break missing intentionally
    				default:
    					$listtype = 'decimal';
    					break;
    			}
    		}
    		else
    		{
    			$listtype = '';
    		}
    
    		// emulates ltrim after nl2br
    		$text = preg_replace('#^(\s|<br>|<br />)+#si', '', $text);
    
    		$bullets = preg_split('#\s*\[\*\]#s', $text, -1, PREG_SPLIT_NO_EMPTY);
    		if (empty($bullets))
    		{
    			return "\n\n";
    		}
    
    		$output = '';
    		foreach ($bullets AS $bullet)
    		{
    			$output .= $this->handle_bbcode_list_element($bullet);
    		}
    
    		if ($listtype)
    		{
    			return '<ol style="list-style-type: ' . $listtype . '">' . $output . '</ol>';
    		}
    		else
    		{
    			return "<ul>$output</ul>";
    		}
    	}
    
    PHP:
     
    !Unreal, Nov 20, 2008 IP