2013-04-30 63 views
1

我有,我有名字的列表的頁面test.php的:PregMach用PHP GET方法

name1='992345' 
name2='332345' 
name3='558645' 
name4='434544' 

test1.php?id=name2另一個頁面,其結果應該是:

我已經試過這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; ?> 

結果是好的

但我wanto能的'/name3=\'的名字從;preg_match_all('/name3=\'([^\']+)\'/',$Text,$Match)用GET方法樣改變這個如果test1.php?id=name4結果將會是蜜蜂434544

+0

'preg_match_all( 「/ $ ID = '([^'] +)'/」,$文本,$匹配); ' – Winston 2013-04-30 07:13:52

+0

你可能想'urldecode($ _ GET ['id']);'去除編碼的字符,例如'+'和'preg_quote($ id)'以防'id'包含正則表達式字符。 \ + *? [^] $(){} =! < > | : - – Waygood 2013-04-30 07:15:49

回答

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

?> 
+0

'<?php $ Text = file_get_contents(「http://portaltv.ro/STREAM.php」); $ id = $ _GET [「id」]; preg_match_all(「/ $ id ='([^'] +)'/」,$ Text,$ Match); $ fid = $匹配[1] [0]; echo $ fid; ?> – 2013-04-30 07:20:44

+1

@PortaltvRomania你想說什麼? – 2013-04-30 07:21:54

+0

它現在正常工作!與thw代碼我張貼erlier! 它適用於您的代碼。謝謝。 – 2013-04-30 07:24:28

1

'(單引號)中,變量被視爲文本而不是變量。

改變這種

preg_match_all('/$id.=\'([^\']+)\'/',$Text,$Match); 

preg_match_all("/$id=\'([^\']+)\'/",$Text,$Match); 
+0

它的好,但沒有從$ id''''' – 2013-04-30 07:22:57

+0

@PortaltvRomania檢查編輯答案。 – 2013-04-30 07:23:36