如何操作和验证包含将由php评估的条件的字符串 [英] How to manipulate and validate string containing conditions that will be evaluated by php

查看:108
本文介绍了如何操作和验证包含将由php评估的条件的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的程序的一部分包括用户创建条件,我将存储在一个mysql数据库。我想要的语法比PHP允许更容易,但由于这将最终作为php代码使用eval()运行它需要有有效的语法。



例如:



使用者输入:

  4< var1< = 9 AND var2< 6 

结果应为:

  4< var1 AND var1< = 9 AND var2< 6 



这是一个很好的方法来检测和拆分比较? p>

此外,如何测试生成的代码不会导致任何php错误,所以如果我可以通知用户?



编辑:
要清楚这些条件可以是任何标准条件,如>,<,> =,<=,==。

ol>
  • 用空格拆分字符串

  • 遍历结果数组

  • t包含AND或OR条件,通过任何逻辑运算符(>,>,> =,<=,==,!=)拆分它们



  • 这将是一个例子(未测试,认为它是伪代码)
    $ tokens = explode('',$ user_string);

      $ to_evaluate = array 
    foreach($ tokens AS $ tok)
    {
    if($ tok!='AND'&& $ tok!='OR')
    {
    $ parts = preg_split(/(< |> |< = |> = | == |!=)+ /,$ tok);
    if(count($ parts)== 3)
    {
    $ to_evaluate [] = array('element1'=> $ parts [0],'condition'=> $ parts [1],'element2'=> $ parts [2]);
    }
    else
    echo¿
    }
    else
    $ to_evaluate [] = $ tok;
    }

    此时,必须遍历$ to_evaluate并开始评估每个找到的条件。


    Part of the program I am creating consists of the user creating conditions which I will store in a mysql database. I want the syntax to be somewhat easier than what php allows but since this will ultimately be run as php code using eval() it needs to have valid syntax.

    For example:

    User Input:

    4<var1<=9 AND var2<6
    

    Result should be:

    4<var1 AND var1<=9 AND var2<6
    

    Before this is eval'd I surround it with an if statement that simply returns true.

    What would be a good way to detect and split that comparison up?

    Also, how can I test that the resulting code will not cause any php errors so that if it will I can inform the user?

    Edit: To be clear those conditions can be any standard conditions such as >,<,>=,<=,==.

    解决方案

    Even a more user friendly approach will be an interface like Treffynnon says, an initial approach to parse the string would be:

    1. split string by spaces (asuming spaces are required)
    2. traverse the resulting array
    3. the items that don't contain AND nor OR are conditions, split them by any of the logic operators (>,>, >=, <=, ==, !=)
    4. Now you got all the sintax parsed, it's ready for interpretation.

    This would be an example (not tested, consider it pseudocode) $tokens = explode(' ', $user_string);

    $to_evaluate = array();
    foreach( $tokens AS $tok )
    {
         if( $tok!='AND' && $tok!='OR' )
         {
             $parts = preg_split("/(<|>|<=|>=|==|!=)+/", $tok);
             if( count($parts)==3 )
             {
                 $to_evaluate[] = array( 'element1'=>$parts[0], 'condition'=>$parts[1], 'element2'=>$parts[2] );
             }
             else
                 echo "¿Error?";
         }
         else
            $to_evaluate[] = $tok;
    }
    

    At this point you must traverse $to_evaluate and start evaluating each condition you found.

    这篇关于如何操作和验证包含将由php评估的条件的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆