草庐IT

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

coder 2024-04-30 原文

我的问题是 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/

有关php - 计数功能在开关中不起作用的更多相关文章

  1. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  2. ruby-on-rails - s3_direct_upload 在生产服务器中不工作 - 2

    在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo

  3. ruby-on-rails - Cucumber 是否只是 rspec 的包装器以帮助将测试组织成功能? - 2

    只是想确保我理解了事情。据我目前收集到的信息,Cucumber只是一个“包装器”,或者是一种通过将事物分类为功能和步骤来组织测试的好方法,其中实际的单元测试处于步骤阶段。它允许您根据事物的工作方式组织您的测试。对吗? 最佳答案 有点。它是一种组织测试的方式,但不仅如此。它的行为就像最初的Rails集成测试一样,但更易于使用。这里最大的好处是您的session在整个Scenario中保持透明。关于Cucumber的另一件事是您(应该)从使用您的代码的浏览器或客户端的角度进行测试。如果您愿意,您可以使用步骤来构建对象和设置状态,但通常您

  4. ruby-on-rails - Ruby on Rails 计数器缓存错误 - 2

    尝试在我的RoR应用程序中实现计数器缓存列时出现错误Unknownkey(s):counter_cache。我在这个问题中实现了模型关联:Modelassociationquestion这是我的迁移:classAddVideoVotesCountToVideos0Video.reset_column_informationVideo.find(:all).eachdo|p|p.update_attributes:videos_votes_count,p.video_votes.lengthendenddefself.downremove_column:videos,:video_vot

  5. ruby - 使用多个数组创建计数 - 2

    我正在尝试按0-9和a-z的顺序创建数字和字母列表。我有一组值value_array=['0','1','2','3','4','5','6','7','8','9','a','b','光盘','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','','u','v','w','x','y','z']和一个组合列表的数组,按顺序,这些数字可以产生x个字符,比方说三个list_array=[]和一个当前字母和数字组合的数组(在将它插入列表数组之前我会把它变成一个字符串,]current_combo['0','0','0']

  6. Unity 3D 制作开关门动画,旋转门制作,推拉门制作,门把手动画制作 - 2

    Unity自动旋转动画1.开门需要门把手先动,门再动2.关门需要门先动,门把手再动3.中途播放过程中不可以再次进行操作觉得太复杂?查看我的文章开关门简易进阶版效果:如果这个门可以直接打开的话,就不需要放置"门把手"如果门把手还有钥匙需要旋转,那就可以把钥匙放在门把手的"门把手",理论上是可以无限套娃的可调整参数有:角度,反向,轴向,速度运行时点击Test进行测试自己写的代码比较垃圾,命名与结构比较拉,高手轻点喷,新手有类似的需求可以拿去做参考上代码usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;u

  7. ruby-on-rails - "assigns"在 Ruby on Rails 中有什么作用? - 2

    我目前正在尝试学习RubyonRails和测试框架RSpec。assigns在此RSpec测试中做什么?describe"GETindex"doit"assignsallmymodelas@mymodel"domymodel=Factory(:mymodel)get:indexassigns(:mymodels).shouldeq([mymodel])endend 最佳答案 assigns只是检查您在Controller中设置的实例变量的值。这里检查@mymodels。 关于ruby-o

  8. ruby-on-rails - rails 功能测试 - 2

    在Rails自动生成的功能测试(test/functional/products_controller_test.rb)中,我看到以下代码:classProductsControllerTest我的问题是:方法调用products()在哪里/如何定义?products(:one)到底是什么意思?看代码,大概意思是“创建一个产品”,但是它是如何工作的呢?注意我是Ruby/Rails的新手,如果这些是微不足道的问题,我深表歉意。 最佳答案 如果您查看test/fixtures文件夹,您会看到一个products.yml文件。这是在您创建

  9. Ruby 计数数组对象,如果对象包含值 - 2

    我有一个数组:array=['Footballs','Baseball','football','Soccer']而且我需要计算看到Football或Baseball的次数,无论大小写和复数形式如何。这是我尝试做的,但没有成功:array.count{|x|x.downcase.include?'football'||x.downcase.include?'baseball'}编写这段代码的正确或更好的方法是什么?我正在寻找3作为答案。 最佳答案 我会将count与一个block结合使用,该block根据与您正在寻找的约束相匹配的正

  10. ruby - 字符串文字前面的 * 在 ruby​​ 中有什么作用? - 2

    这段代码似乎创建了一个范围从a到z的数组,但我不明白*的作用。有人可以解释一下吗?[*"a".."z"] 最佳答案 它叫做splatoperator.SplattinganLvalueAmaximumofonelvaluemaybesplattedinwhichcaseitisassignedanArrayconsistingoftheremainingrvaluesthatlackcorrespondinglvalues.Iftherightmostlvalueissplattedthenitconsumesallrvaluesw

随机推荐