<?php namespace Laravel;
// HTML类
class HTML {

/**
* Convert HTML characters to entities.
* 将 HTML 字符转换为实体。
* The encoding specified in the application configuration file will be used.
* 将使用应用程序配置文件中指定的编码。
* @param string $value
* @return string
*/
public static function entities($value)
{
// htmlentities-将字符串转为HTML转义字符
return htmlentities($value, ENT_QUOTES, Config::get('application.encoding'), false);
}

/**
* Convert entities to HTML characters.
* 将实体转换为 HTML。
* @param string $value
* @return string
*/
public static function decode($value)
{
// html_entity_decode-将HTML实体转为字符串
return html_entity_decode($value, ENT_QUOTES, Config::get('application.encoding'));
}

/**
* Generate a link to a JavaScript file.
* 生成指向 JavaScript 文件的链接
* <code>
* // Generate a link to a JavaScript file
* echo HTML::script('js/jquery.js');
*
* // Generate a link to a JavaScript file and add some attributes
* echo HTML::script('js/jquery.js', array('defer'));
* </code>
*
* @param string $url
* @param array $attributes
* @return string
*/
public static function script($url, $attributes = array())
{
$url = static::entities(URL::to_asset($url));

return '<script src="'.$url.'"'.static::attributes($attributes).'></script>'.PHP_EOL;
}

/**
* Generate a link to a CSS file.
* 生成指向 CSS 文件的链接。
* If no media type is selected, "all" will be used.
* 如果未选择媒体类型,则将使用“全部”。
* <code>
* // Generate a link to a CSS file
* echo HTML::style('css/common.css');
*
* // Generate a link to a CSS file and add some attributes
* echo HTML::style('css/common.css', array('media' => 'print'));
* </code>
*
* @param string $url
* @param array $attributes
* @return string
*/
public static function style($url, $attributes = array())
{
$defaults = array('media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet');

$attributes = $attributes + $defaults;

$url = static::entities(URL::to_asset($url));

return '<link href="'.$url.'"'.static::attributes($attributes).'>'.PHP_EOL;
}

/**
* Generate a HTML span.
* 生成 HTML span。
* @param string $value
* @param array $attributes
* @return string
*/
public static function span($value, $attributes = array())
{
return '<span'.static::attributes($attributes).'>'.static::entities($value).'</span>';
}

/**
* Generate a HTML link.
* 生成 HTML 链接。
* <code>
* // Generate a link to a location within the application
* echo HTML::link('user/profile', 'User Profile');
*
* // Generate a link to a location outside of the application
* echo HTML::link('http://google.com', 'Google');
* </code>
*
* @param string $url
* @param string $title
* @param array $attributes
* @param bool $https
* @return string
*/
public static function link($url, $title, $attributes = array(), $https = false)
{
$url = static::entities(URL::to($url, $https));

return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
}

/**
* Generate a HTTPS HTML link.
* 生成 HTTPS HTML 链接。
* @param string $url
* @param string $title
* @param array $attributes
* @return string
*/
public static function link_to_secure($url, $title, $attributes = array())
{
return static::link($url, $title, $attributes, true);
}

/**
* Generate an HTML link to an asset.
* 生成指向资产的 HTML 链接。
* The application index page will not be added to asset links.
*
* @param string $url
* @param string $title
* @param array $attributes
* @param bool $https
* @return string
*/
public static function link_to_asset($url, $title, $attributes = array(), $https = null)
{
$url = static::entities(URL::to_asset($url, $https));

return '<a href="'.$url.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
}

/**
* Generate an HTTPS HTML link to an asset.
* 生成指向资源的 HTTPS HTML 链接
* @param string $url
* @param string $title
* @param array $attributes
* @return string
*/
public static function link_to_secure_asset($url, $title, $attributes = array())
{
return static::link_to_asset($url, $title, $attributes, true);
}

/**
* Generate an HTML link to a route.
* 生成指向路由的 HTML 链接。
* An array of parameters may be specified to fill in URI segment wildcards.
*
* <code>
* // Generate a link to the "profile" named route
* echo HTML::link_to_route('profile', 'Profile');
*
* // Generate a link to the "profile" route and add some parameters
* echo HTML::link_to_route('profile', 'Profile', array('taylor'));
* </code>
*
* @param string $name
* @param string $title
* @param array $parameters
* @param array $attributes
* @return string
*/
public static function link_to_route($name, $title, $parameters = array(), $attributes = array())
{
return static::link(URL::to_route($name, $parameters), $title, $attributes);
}

/**
* Generate an HTML mailto link.
* 生成 HTML mailto 链接。
* The E-Mail address will be obfuscated to protect it from spam bots.
* 电子邮件地址将被混淆以保护它免受垃圾邮件机器人的攻击。
* @param string $email
* @param string $title
* @param array $attributes
* @return string
*/
public static function mailto($email, $title = null, $attributes = array())
{
$email = static::email($email);

if (is_null($title)) $title = $email;

$email = '&#109;&#097;&#105;&#108;&#116;&#111;&#058;'.$email;

return '<a href="'.$email.'"'.static::attributes($attributes).'>'.static::entities($title).'</a>';
}

/**
* Obfuscate an e-mail address to prevent spam-bots from sniffing it.
* 混淆电子邮件地址以防止垃圾邮件机器人嗅探它。
* @param string $email
* @return string
*/
public static function email($email)
{
return str_replace('@', '&#64;', static::obfuscate($email));
}

/**
* Generate an HTML image element.
* 生成 HTML 图像元素。
* @param string $url
* @param string $alt
* @param array $attributes
* @return string
*/
public static function image($url, $alt = '', $attributes = array())
{
$attributes['alt'] = $alt;

return '<img src="'.static::entities(URL::to_asset($url)).'"'.static::attributes($attributes).'>';
}

/**
* Generate an ordered list of items.
* 生成项目的有序列表。
* @param array $list
* @param array $attributes
* @return string
*/
public static function ol($list, $attributes = array())
{
return static::listing('ol', $list, $attributes);
}

/**
* Generate an un-ordered list of items.
* 生成一个无序的项目列表。
* @param array $list
* @param array $attributes
* @return string
*/
public static function ul($list, $attributes = array())
{
return static::listing('ul', $list, $attributes);
}

/**
* Generate an ordered or un-ordered list.
* 生成有序或无序列表。
* @param string $type
* @param array $list
* @param array $attributes
* @return string
*/
private static function listing($type, $list, $attributes = array())
{
$html = '';

foreach ($list as $key => $value)
{
// If the value is an array, we will recurse the function so that we can
// produce a nested list within the list being built. Of course, nested
// lists may exist within nested lists, etc.
// 如果值是一个数组,我们将递归函数,以便我们可以在正在构建的列表中生成一个嵌套列表。
// 当然,嵌套列表中可能存在嵌套列表等。
if (is_array($value))
{
$html .= static::listing($type, $value);
}
else
{
$html .= '<li>'.static::entities($value).'</li>';
}
}

return '<'.$type.static::attributes($attributes).'>'.$html.'</'.$type.'>';
}

/**
* Build a list of HTML attributes from an array.
* 从数组构建 HTML 属性列表。
* @param array $attributes
* @return string
*/
public static function attributes($attributes)
{
$html = array();

foreach ((array) $attributes as $key => $value)
{
// For numeric keys, we will assume that the key and the value are the
// same, as this will conver HTML attributes such as "required" that
// may be specified as required="required".
// 对于数字键,我们将假设键和值相同,因为这将转换 HTML 属性,
// 例如可能指定为 required="required" 的 "required"。
// is_numberic-检测变量是否为数字或数字字符串
if (is_numeric($key)) $key = $value;

if ( ! is_null($value))
{
$html[] = $key.'="'.static::entities($value).'"';
}
}

return (count($html) > 0) ? ' '.implode(' ', $html) : '';
}

/**
* Obfuscate a string to prevent spam-bots from sniffing it.
* 混淆字符串以防止垃圾邮件机器人嗅探它。
* @param string $value
* @return string
*/
protected static function obfuscate($value)
{
$safe = '';
// str_split 将字符串转为数组
foreach (str_split($value) as $letter)
{
// To properly obfuscate the value, we will randomly convert each
// letter to its entity or hexadecimal representation, keeping a
// bot from sniffing the randomly obfuscated letters from the
// page and guarding against e-mail harvesting.
// 为了正确混淆该值,我们将每个字母随机转换为其实体或十六进制表示,
// 防止机器人从页面嗅探随机混淆的字母并防止电子邮件收集。
// rand 产生一个随机整数
switch (rand(1, 3))
{
case 1:
$safe .= '&#'.ord($letter).';'; // ord 转换字符串第一个字节为 0-255 之间的值
break;

case 2:
$safe .= '&#x'.dechex(ord($letter)).';'; // dechex 十进制转换为十六进制
break;

case 3:
$safe .= $letter;
}
}

return $safe;
}

}

github地址: ​​https://github.com/liu-shilong/laravel3-scr​​