PHP 解析 url 变量
PHP parse url for variables
我正在我的本地主机上构建一个 运行 工具,它有助于更快地将静态网页放在一起。安全不是问题,因为这只会在本地 运行 进行。
首先,我有一个名为 components.php
的 include
文件,其中包含页面部分的变量,如下所示:
$slide="Pretend this is markup for a Slider";
$port="Pretend this is markup for a set of portfolio images";
$para="<p>Just another paragraph</p>";
$h1="<h1>This is a Header</h1>";
然后我的 url 看起来像这样:
//only calling 3 of the 4 sections
localhost/mysite/index.php?sections=h1-slide-para
我的索引文件有这个:
include 'components.php'
$sections = @$_GET['sections'];
$section = explode($sections,"-");
foreach ($section as $row){
echo $row;
}
这里的目标是用我一直使用的行构建 components.php
文件,这样我就可以直接从浏览器的地址栏快速将页面布局放在一起。我只是不确定如何 echo
变量,一旦我 explode
它们使得 index.php
只包含我从 components.php
文件调用的标记。
这应该适合你:
只需使用 variable variables
访问 components.php
文件中的变量(同时切换 explode()
中的参数,它们是错误的方式),例如
$section = explode("-", $sections);
foreach ($section as $row) {
echo $$row;
//^^ See here the double dollar sign
}
另一种解决方案是将您的文件更改为 ini
格式,例如
slide="Pretend this is markup for a Slider"
port="Pretend this is markup for a set of portfolio images"
para="<p>Just another paragraph</p>"
h1="<h1>This is a Header</h1>"
然后用 parse_ini_file()
:
把它放到一个数组中
$arr = parse_ini_file("components.ini");
//^^^ Note, that you now work with an .ini file
$sections = @$_GET['sections'];
$section = explode("-", $sections);
foreach ($section as $row) {
echo $arr[$row];
}
首先代替
$section = explode($sections, "-");
使用
$section = explode("-", $sections);
还有
foreach ($section as $row){
echo eval('return $'. $row . ';');
}
将字符串放入数组中:
$sections = [
'slide' => "Pretend this is markup for a Slider",
'port' => "Pretend this is markup for a set of portfolio images",
'para' => "<p>Just another paragraph</p>",
'h1' => "<h1>This is a Header</h1>",
];
然后,按名称引用这些部分:
foreach (explode('-', $_GET['sections']) as $section){
echo $sections[$section];
}
我正在我的本地主机上构建一个 运行 工具,它有助于更快地将静态网页放在一起。安全不是问题,因为这只会在本地 运行 进行。
首先,我有一个名为 components.php
的 include
文件,其中包含页面部分的变量,如下所示:
$slide="Pretend this is markup for a Slider";
$port="Pretend this is markup for a set of portfolio images";
$para="<p>Just another paragraph</p>";
$h1="<h1>This is a Header</h1>";
然后我的 url 看起来像这样:
//only calling 3 of the 4 sections
localhost/mysite/index.php?sections=h1-slide-para
我的索引文件有这个:
include 'components.php'
$sections = @$_GET['sections'];
$section = explode($sections,"-");
foreach ($section as $row){
echo $row;
}
这里的目标是用我一直使用的行构建 components.php
文件,这样我就可以直接从浏览器的地址栏快速将页面布局放在一起。我只是不确定如何 echo
变量,一旦我 explode
它们使得 index.php
只包含我从 components.php
文件调用的标记。
这应该适合你:
只需使用 variable variables
访问 components.php
文件中的变量(同时切换 explode()
中的参数,它们是错误的方式),例如
$section = explode("-", $sections);
foreach ($section as $row) {
echo $$row;
//^^ See here the double dollar sign
}
另一种解决方案是将您的文件更改为 ini
格式,例如
slide="Pretend this is markup for a Slider"
port="Pretend this is markup for a set of portfolio images"
para="<p>Just another paragraph</p>"
h1="<h1>This is a Header</h1>"
然后用 parse_ini_file()
:
$arr = parse_ini_file("components.ini");
//^^^ Note, that you now work with an .ini file
$sections = @$_GET['sections'];
$section = explode("-", $sections);
foreach ($section as $row) {
echo $arr[$row];
}
首先代替
$section = explode($sections, "-");
使用
$section = explode("-", $sections);
还有
foreach ($section as $row){
echo eval('return $'. $row . ';');
}
将字符串放入数组中:
$sections = [
'slide' => "Pretend this is markup for a Slider",
'port' => "Pretend this is markup for a set of portfolio images",
'para' => "<p>Just another paragraph</p>",
'h1' => "<h1>This is a Header</h1>",
];
然后,按名称引用这些部分:
foreach (explode('-', $_GET['sections']) as $section){
echo $sections[$section];
}