gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 00:45:00 24 4
gpt4 key购买 nike

我的问题是 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";
}

我知道计数函数和 $matches 有效。这是我现在有的两页供引用。第一页:

<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>

最佳答案

试试这个,你已经在所有 case 中使用了 case count($matches[1])

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>

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 />";
}
}
?>

关于php - 计数功能在开关中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21436731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com