使用php GET方法的PregMach =

时间:2013-05-24 14:11:23

标签: php

我有一个页面test.php,其中有一个名单列表:

  

NAME1 = 992345&安培;名称2 = 332345&安培; NAME3 = 558645&安培; NAME4 = 434544

在另一页test1.php?id = name2中,结果应为:

33234

我已经尝试过这个PHP代码:

<?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 "";
}

 ?>

但它不起作用。

如果列表中包含''

  

NAME1 = '992345' &安培; NAME2 = '332345' &安培; NAME3 = '558645' &安培; NAME4 = '434544'

它有效。如何改变工作?

2 个答案:

答案 0 :(得分:2)

parse_str($Text,$data);
echo $data[$_GET["id"]];

parse_str

答案 1 :(得分:-1)

<?php 
  $Text=file_get_contents("test.php");
  if(isset($_GET["id"])){
     $id = $_GET["id"];
     parse_str($Text,$data);
     echo $data[$id];
  } else {
     echo "";
  }

?>

此代码应该有效。