我有一个页面test.php,其中有一个名单列表:
name1='992345'
name2='332345'
name3='558645'
name4='434544'
在另一个页面test1.php?id=name2
中,结果应为:
332345
我已经尝试过这个PHP代码:
<?php
$Text=file_get_contents("test.php")
$id = $_GET["id"];
;preg_match_all('/$id.=\'([^\']+)\'/',$Text,$Match);
$fid=$Match[1][0];
echo $fid; ?>
如果代码是这样使用的
<?php
$Text=file_get_contents("test.php")
;preg_match_all('/name3=\'([^\']+)\'/',$Text,$Match);
$fid=$Match[1][0];
echo $fid; ?>
结果很好
558645
但我希望能够使用GET方法更改'/name3=\'
的{{1}}名称,如果;preg_match_all('/name3=\'([^\']+)\'/',$Text,$Match)
结果为test1.php?id=name4
。
答案 0 :(得分:1)
在'
(单引号)中,变量被视为文本而不是变量。
更改此
preg_match_all('/$id.=\'([^\']+)\'/',$Text,$Match);
到
preg_match_all("/$id=\'([^\']+)\'/",$Text,$Match);
答案 1 :(得分:0)
试试这段代码:
<?php
$Text=file_get_contents("test.php");
$id = $_GET["id"];
$regex = "/".$id."=\'([^\']+)\'/";
preg_match_all($regex,$Text,$Match);
$fid=$Match[1][0];
echo $fid; ?>
这样可行。
修改强>
<?php
$Text=file_get_contents("test.php");
if(isset($_GET["id"])){
$id = $_GET["id"];
$regex = "/".$id."=\'([^\']+)\'/";
preg_match_all($regex,$Text,$Match);
$fid=$Match[1][0];
echo $fid;
} else {
echo "There is no name selected.";
}
?>