【问题标题】:how to use variable names that are stored in SQL tables in php如何在 php 中使用存储在 SQL 表中的变量名
【发布时间】:2016-09-21 21:10:07
【问题描述】:

我有类似的东西来自我的 sql 查询。我正在存储变量名称,希望我可以在 php 中传递变量值,但它无法识别这些值。

[{"dataField":"711","caption":"Product Gas Type","max_length":2,"min_length":0,"validationRules":[{"type":"pattern","pattern":"^[a-zA-Z0-9.]{'.$minVal.','.$maxVal.'}$","message":"This field only accepts letters and numbers. The lentgh has to be between '. $minVal .' and '. $maxVal .'"}]},{"dataField":"712","caption":"1St Charge Charging Point","max_length":2,"min_length":0,"validationRules":[{"type":"pattern","pattern":"^[a-zA-Z0-9.]{'.$minVal.','.$maxVal.'}$","message":"This field only accepts letters and numbers. The lentgh has to be between '. $minVal .' and '. $maxVal .'"}]}]

我想说:

   $minVal=1; 
   $maxVal=4;

但我的 $result 变量永远不会识别变量(最小和最大 vals)值。我怎么能做这样的事情?我理想的最终结果是这样的:

   [{"dataField":"711","caption":"Product Gas Type","max_length":2,"min_length":0,"validationRules":[{"type":"pattern","pattern":"^[a-zA-Z0-9.]{1,4}$","message":"This field only accepts letters and numbers. The lentgh has to be between 1 and 4"}]},{"dataField":"712","caption":"1St Charge Charging Point","max_length":2,"min_length":0,"validationRules":[{"type":"pattern","pattern":"^[a-zA-Z0-9.]{1,4}$","message":"This field only accepts letters and numbers. The lentgh has to be between 1 and 4"}]}]

我已尝试执行以下操作:

 print_r(str_replace('$minVal', $minVal,$result[$i]->validationRules[0]->pattern));

这会打印正确的东西,但它不会改变我的原始变量 ($result),而且当我尝试为 $maxVal 添加相同的变量时,它会将 $minVal 替换为单词 $maxVal

我也尝试过使用 pre_replace 但它甚至不会打印任何东西

我认为问题可能是这个变量名在 $result 内,而在 result 内有 validationRules 数组,所以也许这就是字符串替换无法访问它们并更改整个原始数组的原因

任何帮助将不胜感激!

【问题讨论】:

  • 您必须使用eval(),但这会变得非常有问题。我建议您考虑使用模板库。
  • str_replace() 方法应该可以工作。但是您不应该在 SQL 数据中使用 . 连接运算符。 SQL 数据不作为 PHP 代码执行,所以你不应该有运算符、引号等。
  • 您可以使用 sprintf (example),但是当有多个重复变量时,您将开始必须实现或使用模板引擎。

标签: php sql arrays preg-replace str-replace


【解决方案1】:

原始输出只是纯文本,不会将变量识别为子字符串。你可以试试这样丑陋的东西(它会起作用):

$raw_output = '{"dataField":"711","caption":"Product Gas Type","max_length":2,"min_length":0,"validationRules":[{"type":"pattern","pattern":"^[a-zA-Z0-9.]{'.$minVal.','.$maxVal.'}$","message":"This field only accepts letters and numbers. The lentgh has to be between '. $minVal .' and '. $maxVal .'"}]},{"dataField":"712","caption":"1St Charge Charging Point","max_length":2,"min_length":0,"validationRules":[{"type":"pattern","pattern":"^[a-zA-Z0-9.]{'.$minVal.','.$maxVal.'}$","message":"This field only accepts letters and numbers. The lentgh has to be between '. $minVal .' and '. $maxVal .'"}]}';

然后在 $minVal 字符串上爆炸:

$explode = explode("' . $minVal . '", $raw_output);
$count = count($explode);
$count_minus_one = $count - 1;
$processed_output = '';
for($i = 0; $i < $count_minus_one; $i++) {
    $processed_output .= $explode[$i] . $minVal;
}
$processed_output .= $explode[$count_minus_one];

然后重复 $maxVal。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 2018-12-28
    • 2016-06-16
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多