2017-07-07 84 views
1

我需要輸入來自輸入字段的值。優選地,$ var名稱可以與輸入字段名相同。示例:echo $yesRedirect;應該輸出:NoPHP回聲輸入來自HTML的值

<?php 
$html = array('<input type="text" name="yesRedirect" id="yesRedirectfunction" value="No">', 
'<input type="text" name="noRedirect" id="noRedirectfunction" value="Yes">', 
'<input type="text" name="quickcheck" id="quickcheckfunction" value="No">'); 

$result = array(); 

foreach ($html as $name) { 
$dom = new DomDocument; 
$dom->loadHTML($name); 
var_dump($dom) . "\n"; 

//echo $dom->getAttribute('name') . "\n"; 
//echo $dom->getAttribute('value') . "\n"; 
} 

echo $yesRedirect; // Should output: No 
echo $noRedirect; // Should output: Yes 
?> 

我怎麼能簡單地echo輸入值?有沒有其他方法? 謝謝你的幫助。

回答

0

我得到的結果與下面這段代碼想:

<?php 

$html = array('<input type="text" name="yesRedirect" id="yesRedirectfunction" value="No">', 
       '<input type="text" name="noRedirect" id="noRedirectfunction" value="Yes">', 
       '<input type="text" name="quickcheck" id="quickcheckfunction" value="No">'); 

$result = array(); 

foreach ($html as $name) { 
$name = str_replace('"','',$name); 
$name = str_replace('>','',$name); 

$varname = explode("name=", $name); 
$arrname = explode(' ',trim($varname['1'])); 

$varvalue = explode("value=", $name); 
$arrvalue = explode(' ',trim($varvalue['1'])); 

$result[$arrname[0]] = $arrvalue[0]; 

} 

echo $result['yesRedirect']; 
echo $result['noRedirect']; 
echo $result['quickcheck']; 
?>