我使用PHP HereDoc和NowDoc语句在我的网站中构建网页,HereDocs用于需要将PHP变量值替换到网页中的部分,NowDocs用于使用$字符在我的JavaScript中指定jQuery语句和jQuery对象变量的部分。然而,这使得我的HTML、CSS和JavaScript/jQuery很难阅读和维护。
为了解决这个问题,我想我应该写一个替换函数来执行HereDoc语句所做的PHP变量替换,但对于那些用字符串表达式或NowDoc语句赋值的变量。这样,字符串中${variable-name}指定的PHP变量将被它们的值替换,并且jQuery语句和值应为jQuery对象的带有前缀的$$变量不会被替换为PHP变量,当没有名称与jQuery语句或变量名匹配的PHP变量时,通常会导致PHP编译器错误,或者当名称匹配时会导致运行时逻辑错误。
这是我的代码和一个测试NowDoc,它:
- 将包含${test}PHP变量的长字符串分配给PHP数组变量中的元素,然后
- 将数组作为参数传递给我的NowHereDoc函数,以执行PHP变量/值替换
但是,当我运行以下代码来构建网页时,$test PHP变量在函数内部不可见,并且用NULL代替了所需的值。
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<?php
//
// PHP
//
function NowHereDoc( &$_array_ ) { // By Ref array parameter minimizes data movement when calling this function.
$_l_ = count( $_array_ );
$_s_ = $_array_[ $_l_ - 1 ]; // Get the last element's string value.
// Find each ${...} and substitute the variable's value ...
$_i1_ = 0;
while( ( $_i1_ = strpos( $_s_, '${', $_i1_ ) ) !== FALSE ) { // Get index of start of a variable name specified
// by ${...} or FALSE when there aren't any {more},
// then stop looping.
$_i2_ = strpos( $_s_, '}', $_i1_ ); // Get index of end of the ${...} just found.
$_l_ = $_i2_ - $_i1_ + 1; // Get length of ${...}.
$_var_ = substr( $_s_, $_i1_, $_l_ ); // Get the variable's name as a string.
$_v_ = str_replace( [ '{', '}' ], '', $_var_ ); // Remove { and } from the variable name.
$_val_ = "$_v_"; // Get the value of the specified variable -- this
// doesn't find the variable, instead returns NULL!
// Substitute the variable's value into the original string, $_s_
// $_s_ = substr_replace( $_s_, $$_var_, $_i1_, $_l_ ); // Replace the single occurance of the variable
// with its value. This could to replace occurances
// not with in comments, not yet implemented in
// function.
$_s_ = str_replace( $_var_, $_val_, $_s_ ); // Replace all occurances of the variable with its
// value.
} // End of while( ( $_i1_ = strpos( $_s_, '${', $_i1_ ) ) !== FALSE ) ...
$_array_[ $_l_ - 1 ] = $_s_; // Set the last element's string value to the
// updated string in $_s_.
}
// No Variable substitutions allowed in a NowDoc.
$strMAINs[] = <<<'MAIN'
<!-- ======================================================
======================================================
=
= HTML - ${test} - This should be substituted with hi
=
======================================================
====================================================== -->
<p>to be set by this following script</p>
<script id="scriptId">
//
// JavaScript!
//
// The following jQuery statement shouldn't be changed.
//
var $jQVar = $( 'p' ).html( 'there' );
</script>
MAIN;
//
// PHP
//
$test = 'hi';
NowHereDoc( $strMAINs );
?>
<!-- HTML -->
<b>Test of my PHP NowHereDoc function</b>
如何通过指定为字符串值的PHP变量的名称访问PHP变量,这些名称在从函数中调用NowHereDoc函数的函数中是全局或局部的,而不必将它们作为参数传入或指定为函数中的全局变量?我记得看到一个PHP库函数,它可以以字符串的形式返回一个给定名称的值,但我不记得函数的名称了。
感谢
要将全局变量导入函数,可以执行以下操作:
// global scope
$test = 'my string';
function myFunction() {
global $test;
}
或者,如果你有一个匿名函数,你可以从父作用域中提取变量,如下所示:
function myOuterFn() {
$test = 'my string';
$myInnerFn = function() use ($test) {
echo $test;
};
}
作为对您评论的回应,如果您循环浏览您的内容以将任何引用的变量提取到一个var名称数组中,您可以让它们循环浏览该数组并从全局导入它们,如本例所示:https://3v4l.org/rH8J0
虽然不是一个模板解决方案,稍后可能会出现,但我发现了get_defined_vars((函数,这是我记得看到过的部分,但记不起它的名称。它获取一个已定义变量的列表,这些变量在调用时处于作用域中。或者,可以创建手动变量列表并将其传递到函数中,因为不需要get_define_var((函数返回的已定义变量列表中的大多数变量。
function NowHereDoc( &$_vars_, &$_array_ ) { // a by-ref array of variables and their values from the caller's scope.
//
// a by-ref array specifying the web-content as a string that
// contains one or more PHP variables in the ${...} format who's
// values are to be substituted for the variable name.
//
// & By-Ref prefix minimizes data movement when calling this function.
//
$_l_ = count( $_array_ );
$_s_ = $_array_[ $_l_ - 1 ]; // Get the last element's string value.
// Find each ${...} and substitute the variable's value ...
$_i1_ = 0;
while( ( $_i1_ = strpos( $_s_, '${', $_i1_ ) ) !== FALSE ) { // Get index of start of a variable name specified
// by ${...} or FALSE when there aren't any {more},
// then stop looping.
$_i2_ = strpos( $_s_, '}', $_i1_ ); // Get index of end of the ${...} just found.
$_l_ = $_i2_ - $_i1_ + 1; // Get length of ${...}.
$_var_ = substr( $_s_, $_i1_, $_l_ ); // Get the variable's name as a string.
$_v_ = str_replace( [ '$', '{', '}' ], '', $_var_ ); // Remove { and } from the variable name.
$_val_ = $_vars_[ $_v_ ]; // Get the value of the specified variable.
// Substitute the variable's value into the original string, $_s_
// $_s_ = substr_replace( $_s_, $_val_, $_i1_, $_l_ ); // Replace the single occurance of the variable
// with its value. This could to replace occurances
// not with in comments, not yet implemented in
// function.
$_s_ = str_replace( $_var_, $_val_, $_s_ ); // Replace all occurances of the variable with its
// value.
} // End of while( ( $_i1_ = strpos( $_s_, '${', $_i1_ ) ) !== FALSE ) ...
$_array_[ $_l_ - 1 ] = $_s_; // Set the last element's string value to the
// updated string in $_s_.
}
// No variable substitutions.
$strMAINs[] = <<<'MAIN'
<!--
=======================================================================
=======================================================================
=
= ${test1} ${test2} // ${test1} ${test2} should be substituted with hi there
=
=======================================================================
=======================================================================
-->
MAIN;
$test1 = 'hi';
$test2 = 'there';
$vars = get_defined_vars();
NowHereDoc( $vars, $strMAINs );
再次感谢马特和CBroe的帮助。