【php开源代码栏目提醒】:网学会员php开源代码为您提供flay.php参考,解决您在flay.php学习中工作中的难题,参考学习。
<?php
/* SVN FILE: $Id: flay.php 4409 2007-02-02 13:20:59Z phpnut $ */
/**
* Text-to-HTML parser.
*
* Text-to-html parser, similar to {@link http://textism.com/tools/textile/ Textile} or {@link http://www.whytheluckystiff.net/ruby/redcloth/ RedCloth}.
*
* PHP versions 4 and 5
*
* CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
* Copyright 2005-2007, Cake Software Foundation, Inc.
* 1785 E. Sahara Avenue, Suite 490-204
* Las Vegas, Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
* @link http://www.cakefoundation.org/projects/info/cakephp Cake
PHP(tm) Project
* @package cake
* @subpackage cake.cake.libs
* @since CakePHP(tm) v 0.2.9
* @version $Revision: 4409 $
* @modifiedby $LastChangedBy: phpnut $
* @lastmodified $Date: 2007-02-02 07:20:59 -0600 (Fri, 02 Feb 2007) $
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*/
/**
* Included libraries.
*
*/
if (!class_exists('Object')) {
uses ('object');
}
/**
* Text-to-HTML parser.
*
* Text-to-html parser, similar to Textile or RedCloth, only with a little different syntax.
*
* @package cake
* @subpackage cake.cake.libs
*/
class Flay extends Object{
/**
* Text to be parsed.
*
* @var string
*/
var $text = null;
/**
* Set this to allow HTML in the markup.
*
* @var boolean
*/
var $allow_html = false;
/**
* Constructor.
*
* @param string $text
*/
function __construct($text = null) {
$this->text = $text;
parent::__construct();
}
/**
* Returns given text translated to
HTML using the Flay syntax.
*
* @param string $text String to format
* @param boolean $bare Set this to only do <p> transforms and > to >, no typography additions.
* @param boolean $allowHtml Set this to trim whitespace and disable all HTML
* @return string Formatted text
*/
function toHtml($text = null, $bare = false, $allowHtml = false) {
if (empty($text) && empty($this->text)) {
return false;
}
$text = $text ? $text : $this->text;
// trim whitespace and disable all HTML
if ($allowHtml) {
$text = trim($text);
} else {
$text = str_replace('<', '<', str_replace('>', '>', trim($text)));
}
if (!$bare) {
// multi-paragraph functions
$text=preg_replace('#(?:[\n]{0,2})"""(.*)"""(?:[\n]{0,2})#s', "\n\n%BLOCKQUOTE%\n\n\\1\n\n%ENDBLOCKQUOTE%\n\n", $text);
$text=preg_replace('#(?:[\n]{0,2})===(.*)===(?:[\n]{0,2})#s', "\n\n%CENTER%\n\n\\1\n\n%ENDCENTER%\n\n", $text);
}
// pre-parse newlines
$text=preg_replace("#\r\n#", "\n", $text);
$text=preg_replace("#[\n]{2,}#", "%PARAGRAPH%", $text);
$text=preg_replace('#[\n]{1}#', "%LINEBREAK%", $text);
$out ='';
foreach(split('%PARAGRAPH%', $text)as $line) {
if ($line) {
if (!$bare) {
$links = array();
$regs = null;
if (preg_match_all('#\[([^\[]{4,})\]#', $line, $regs)) {
foreach($regs[1] as $reg) {
$links[] = $reg;
$line = str_replace("[{$reg}]", '%LINK' . (count($links) - 1) . '%', $line);
}
}
// bold
$line = ereg_replace("\*([^\*]*)\*", "<strong>\\1</strong>", $line);
// italic
$line = ereg_replace("_([^_]*)_", "<em>\\1</em>", $line);
}
// entities
$line = str_replace(' - ', ' – ', $line);
$line = str_replace(' -- ', ' — ', $line);
$line = str_replace('(C)', '©', $line);
$line = str_replace('(R)', '®', $line);
$line = str_replace('(TM)', '™', $line);
// guess e-mails
$emails = null;
if (preg_match_all("#([_A-Za-z0-9+-+]+(?:\.[_A-Za-z0-9+-]+)*@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)*)#", $line, $emails)) {
foreach($emails[1] as $email) {
$line = str_replace($email, "<a href=\"mailto:{$email}\">{$email}</a>", $line);
}
}
if (!$bare) {
$urls = null;
if (preg_match_all("#((?:http|https|ftp|nntp)://[^ ]+)#", $line, $urls)) {
foreach($urls[1] as $url) {
$line = str_replace($url, "<a href=\"{$url}\">{$url}</a>", $line);
}
}
if (preg_match_all("#(www\.[^\n\%\ ]+[^\n\%\,\.\ ])#", $line, $urls)) {
foreach($urls[1] as $url) {
$line = str_replace($url, "<a href=\"http://{$url}\">{$url}</a>", $line);
}
}
if (count($links)) {
for($ii = 0; $ii < count($links); $ii++) {
if (preg_match("#^(http|https|ftp|nntp)://#", $links[$ii])) {
$prefix = null;
} else {
$prefix = 'http://';
}
if (preg_match('#^[^\ ]+\.(jpg|jp