使用 s9e\TextFormatter 的未定义变量 $parser
undefined variable $parser using s9e\TextFormatter
编辑:这不是关于未定义变量的一般性问题,而是关于这个特定代码示例的问题,它在不指定从何处提取变量。
我正在尝试使用 s9e\TextFormatter 设置 HTML 标签白名单,如 here 所述。
这是我的代码:
use s9e\TextFormatter\Configurator;
function htmlFormat( )
{
$configurator = new Configurator;
$configurator->plugins->load( 'HTMLElements' );
$configurator->HTMLElements->allowElement( 'b' );
$configurator->HTMLElements->allowAttribute( 'b', 'class' );
$configurator->HTMLElements->allowElement( 'i' );
// Get an instance of the parser and the renderer
extract( $configurator->finalize() );
$text = '<b>Bold</b> and <i>italic</i> are allowed, but only <b class="important">bold</b> can use the "class" attribute, not <i class="important">italic</i>.';
$xml = $parser->parse( $text );
$html = $renderer->render( $xml );
}
htmlFormat();
然而,变量 $parser
和 $renderer
从未在该示例代码中定义。我不知道如何将它们集成到这段代码中,是吗?
这一行
extract( $configurator->finalize() );
定义那些变量。这是因为 extract() will "Import variables into the current symbol table from an array"1 (referring to the PHP documentation example might help with understanding this). Look at the docblock for Configurator::finalize():
/**
* Finalize this configuration and return all the relevant objects
*
* Options: (also see addHTMLRules() options)
*
* - addHTML5Rules: whether to call addHTML5Rules()
* - finalizeParser: callback executed after the parser is created (gets the parser as arg)
* - finalizeRenderer: same with the renderer
* - optimizeConfig: whether to optimize the parser's config using references
* - returnParser: whether to return an instance of Parser in the "parser" key
* - returnRenderer: whether to return an instance of Renderer in the "renderer" key
*
* @param array $options
* @return array One "parser" element and one "renderer" element unless specified otherwise
*/
最后两个选项(returnParser 和 returnRenderer)默认为真。
尝试运行这些行(在配置配置器实例之后):
extract( $configurator->finalize() );
echo 'typeof $parser: '.get_class($parser).'<br>';
echo 'typeof $renderer: '.get_class($renderer).'<br>';
这应该会产生以下文本:
typeof $parser: s9e\TextFormatter\Parser
typeof $renderer: s9e\TextFormatter\Renderers\XSLT
2https://github.com/s9e/TextFormatter/blob/master/src/Configurator.php#L223
编辑:这不是关于未定义变量的一般性问题,而是关于这个特定代码示例的问题,它在不指定从何处提取变量。
我正在尝试使用 s9e\TextFormatter 设置 HTML 标签白名单,如 here 所述。
这是我的代码:
use s9e\TextFormatter\Configurator;
function htmlFormat( )
{
$configurator = new Configurator;
$configurator->plugins->load( 'HTMLElements' );
$configurator->HTMLElements->allowElement( 'b' );
$configurator->HTMLElements->allowAttribute( 'b', 'class' );
$configurator->HTMLElements->allowElement( 'i' );
// Get an instance of the parser and the renderer
extract( $configurator->finalize() );
$text = '<b>Bold</b> and <i>italic</i> are allowed, but only <b class="important">bold</b> can use the "class" attribute, not <i class="important">italic</i>.';
$xml = $parser->parse( $text );
$html = $renderer->render( $xml );
}
htmlFormat();
然而,变量 $parser
和 $renderer
从未在该示例代码中定义。我不知道如何将它们集成到这段代码中,是吗?
这一行
extract( $configurator->finalize() );
定义那些变量。这是因为 extract() will "Import variables into the current symbol table from an array"1 (referring to the PHP documentation example might help with understanding this). Look at the docblock for Configurator::finalize():
/** * Finalize this configuration and return all the relevant objects * * Options: (also see addHTMLRules() options) * * - addHTML5Rules: whether to call addHTML5Rules() * - finalizeParser: callback executed after the parser is created (gets the parser as arg) * - finalizeRenderer: same with the renderer * - optimizeConfig: whether to optimize the parser's config using references * - returnParser: whether to return an instance of Parser in the "parser" key * - returnRenderer: whether to return an instance of Renderer in the "renderer" key * * @param array $options * @return array One "parser" element and one "renderer" element unless specified otherwise */
最后两个选项(returnParser 和 returnRenderer)默认为真。
尝试运行这些行(在配置配置器实例之后):
extract( $configurator->finalize() );
echo 'typeof $parser: '.get_class($parser).'<br>';
echo 'typeof $renderer: '.get_class($renderer).'<br>';
这应该会产生以下文本:
typeof $parser: s9e\TextFormatter\Parser
typeof $renderer: s9e\TextFormatter\Renderers\XSLT
2https://github.com/s9e/TextFormatter/blob/master/src/Configurator.php#L223