标记/输出

用于轻松构建格式化 php 模板的简洁输出函数。

1.0.3 2014-06-27 18:56 UTC

This package is not auto-updated.

Last update: 2024-09-24 01:46:13 UTC


README

由于需要轻松构建正确格式的 php 模板,out 库为所有 HTML5 上下文(文本、HTML、脚本、样式和 CDATA)提供简洁的输出函数。它还通过假定将在所有地方使用 UTF-8 编码,并将所有无效字符替换(或删除)为 Unicode 替换字符“�”,以确保字符编码的一致性。

Build Status

示例

<?php

// blog post submitted by user
$userName  = '</script> I am an xss attacker';
$postTitle = 'I pwn you <script>pwn(home)</script>';
$postBody  = '<p>This html block <em>MUST</em> be well scrubbed or come from a trusted source.</p>';
$customCss = 'background:black;color:white;</style> XSS here';
$cdata     = 'Who uses this?';

?>
<!DOCTYPE html>
<html>
<head>
  <title><?php out\text(sprintf(_('Blog post: %s'), $postTitle)) ?></title>
  <style>
    <?php out\style($customCss) ?>
  </style>
</head>
<body>
  <h1><?php out\text($postTitle) ?></h1>
  <div id="post-body">
    <?php out\html($postBody) ?>
  </div>
  <script>
    initApp(<?php out\script(json_encode($userName))) ?>)
  </script>
  <![CDATA[<?php out\cdata($cdata) ?>]]>
</body>
</html>

安装

从 Packagist 将其添加到 composer.json

composer require tagged/out:*

out 库包含在 composer 自动加载器中。

require 'vendor/autoload.php';

使用方法

输出函数

所有输出函数都直接写入 stdout。

使用 out\text 写入转义后的 HTML 文本。

<h1>Hello <?php out\text($name) ?></h1>

<img src="<?php out\text($image_url) ?>">

使用 out\html 写入原始 HTML。

<div id="content">
    <?php out\html($content_html) ?>
</div>

使用 out\script 将数据写入脚本块。

<script>
    var data = <?php out\script(json_encode($data)) ?>;
</script>

使用 out\style 将数据写入样式块。

<style>
    <?php out\style($css) ?>
</style>

使用 out\cdata 将数据写入 CDATA 块。

<![CDATA[
    <?php out\cdata($character_data) ?>
]]>

字符串函数

所有字符串函数都以字符串形式返回结果。每个输出函数都有一个相应的字符串函数。

$encodedName = out\stext($name);
$content     = out\shtml($content_html);
$scriptData  = out\sscript(json_encode($data));
$styleData   = out\sstyle($css);
$cdataData   = out\scdata($character_data);

测试

phpunit