计数功能在开关中不起作用

Ski*_*345 3 php switch-statement

我的问题是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";
}
Run Code Online (Sandbox Code Playgroud)

我知道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>
Run Code Online (Sandbox Code Playgroud)

第二页:

<?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>
Run Code Online (Sandbox Code Playgroud)

Kri*_*h R 5

试试这个,你已经习惯case count($matches[1])了case

switch(count($matches[1]))
{
 case  1:
...
Run Code Online (Sandbox Code Playgroud)

代替

switch(count($matches[1]))
{
case count($matches[1]) = 1:
Run Code Online (Sandbox Code Playgroud)

更新:

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>
Run Code Online (Sandbox Code Playgroud)

在emailform.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 />";          
    }   
  }
 ?>
Run Code Online (Sandbox Code Playgroud)