我的问题是count函数在这个switch语句中不起作用,我不知道为什么。我知道可能有更好的方法来做到这一点,但我只是想特别想弄清楚这一点。
switch(count($matches[1]))
{
case count($matches[1]) = 1:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />";
break;
case count($matches[1]) = 2:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />
$matches[1][1] <input type=\"text\" name=\"$matches[1][1]\" /><br />";
break;
case count($matches[1]) = 3:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />
$matches[1][1] <input type=\"text\" name=\"$matches[1][1]\" /><br />
$matches[1][2] <input type=\"text\" name=\"$matches[1][2]\" /><br />";
break;
default:
print "Error";
}
我知道count函数和$匹配工作。 以下是我现在有两个页面供参考。 第一页:
<form action="emailform.php" method="post">
<textarea rows="20" cols="20" name="template[]"></textarea>
<input type="submit" name="submit" value="Store your template here" />
</form>
<p>This is where you enter your standard email.<br />
The words that need to change every time are variables using the parenthesis {{}} i.e.
{{Customer name}}, {{item}}, {{price}}</p>
第二页:
<?php
$pattern = "/\{{2}([a-zA-Z]*)\}{2}/";
$subject = $_POST["template"];
preg_match_all($pattern, $subject, $matches);
echo "<pre>";
var_dump($matches);
echo "</pre>";
echo count($matches[1]);
?>
<form action="emailform.php" method="post">
<?php
switch(count($matches[1]))
{
case count($matches[1]) = 1:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />";
break;
case count($matches[1]) = 2:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />
$matches[1][1] <input type=\"text\" name=\"$matches[1][1]\" /><br />";
break;
case count($matches[1]) = 3:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />
$matches[1][1] <input type=\"text\" name=\"$matches[1][1]\" /><br />
$matches[1][2] <input type=\"text\" name=\"$matches[1][2]\" /><br />";
break;
case count($matches[1]) = 4:
print "$matches[1][0] <input type=\"text\" name=\"$matches[1][0]\" /><br />
$matches[1][1] <input type=\"text\" name=\"$matches[1][1]\" /><br />
$matches[1][2] <input type=\"text\" name=\"$matches[1][2]\" /><br />
$matches[1][3] <input type=\"text\" name=\"$matches[1][3]\" />";
break;
default:
print "Error";
}
?>
<input type="submit" name="submit" />
</form>
答案 0 :(得分:5)
试试这个,您在所有case count($matches[1])
case
switch(count($matches[1]))
{
case 1:
...
而不是
switch(count($matches[1]))
{
case count($matches[1]) = 1:
<强>更新强>
HTML:
<form action="emailform.php" method="post">
<textarea rows="20" cols="20" name="template">Dear {{customer}}, Your {{item}} will cost price. Thank you.</textarea>
<input type="submit" name="submit" value="Store your template here" />
</form>
phpform.php中的PHP
<?php
if(isset($_POST['submit'])){
$pattern = "/\{{2}([a-zA-Z]*)\}{2}/";
echo $subject = $_POST["template"];
preg_match_all($pattern, $subject, $matches);
echo "<pre>";
var_dump($matches);
echo "</pre>";
foreach($matches[1] as $key=>$value){
print $value ."<input type=\"text\" name=\"$value\" /><br />";
}
}
?>