我需要从输入字段中回显值。 $ var 名称最好与输入字段名相同。示例:echo $yesRedirect;
应输出:No
<?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
?>
如何简单地回显输入值?还有其他方法吗? 谢谢你的帮助。
答案 0 :(得分: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'];
?>