Location: PHPKode > projects > Shinobu > system/lib/markup_parsers/CommentParser.php
<?php

/* ---

	Copyright (C) 2008-2009 Frank Smit
	http://shinobu.61924.nl/

	This file is part of Shinobu.

	Shinobu is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Shinobu is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Shinobu. If not, see <http://www.gnu.org/licenses/>.

--- */

function parse($str)
{
	$parser = new CommentParser;
	return $parser->parse($str);
}

class CommentParser
{
	protected $hashes = array('hash'=>array(), 'html'=>array());

	public function parse($text)
	{
		// Make safe
		$text = str_replace(array("\r\n", "\r"), "\n", $text);
		$text = preg_replace('/^[ ]+$/m', '', $text);

		// Encode html characters
		$text = utf8_htmlencode($text);

		// Make hashes of links and urls
		$pattern = '/\[((http|https|ftp|irc):\/\/[a-zA-Z0-9._%-?&\/:\#~=;@\+\(\),\{\}\\\^\[\]`<>"!\']+)[ ](.*?)\]/i';
		$text = preg_replace_callback($pattern, array(&$this, 'cb_hash_links'), $text);

		$pattern = '/((http|https|ftp|irc):\/\/[a-zA-Z0-9._%-?&\/:\#~=;@\+\(\),\{\}\\\^\[\]`<>"!\']+)/i';
		$text = preg_replace_callback($pattern, array(&$this, 'cb_hash_urls'), $text);

		// Handle whitespaces
		$text = str_replace(array("\n", "\t", '  ', '  '), array('<br />', '&nbsp; &nbsp; ', '&nbsp; ', ' &nbsp;'), $text);

		// Replace hashes
		if (count($this->hashes['hash']) > 0)
			$text = str_replace($this->hashes['hash'], $this->hashes['html'], $text);

		// Add paragraph tag around text, but make sure there are no empty paragraphs
		$text = preg_replace('#<br />\s*?<br />((\s*<br />)*)#is', "</p>$1<p>", $text);
		$text = str_replace('<p><br />', '<p>', $text);
		$text = str_replace('<p></p>', '', '<p>'.$text.'</p>');

		return $text;
	}

	/* --- Callback functions
	------------------------------------------------- */

	protected function cb_hash_links($matches)
	{
		$link_hash = md5($matches[0].mt_rand());
		$this->hashes['hash'][] = $link_hash;

		if (!in_array(get_ext($matches[5]), array('png', 'jpg', 'gif', 'jpeg')))
			$this->hashes['html'][] = '<a href="'.$matches[3].'">'.$matches[5].'</a>';
		else
			$this->hashes['html'][] = '<a href="'.$matches[3].'"><img src="'.$matches[5].'" alt="" /></a>';

		return $link_hash;
	}

	protected function cb_hash_urls($matches)
	{
		$url_hash = md5($matches[0].mt_rand());
		$this->hashes['hash'][] = $url_hash;

		if (!in_array(get_ext($matches[0]), array('png', 'jpg', 'gif', 'jpeg')))
			$this->hashes['html'][] = '<a href="'.$matches[0].'">'.$matches[0].'</a>';
		else
			$this->hashes['html'][] = '<img src="'.$matches[0].'" alt="" />';

		return $url_hash;
	}
}

?>
Return current item: Shinobu