我有一个始终遵循以下格式的字符串:

This Fee Name :  *  Fee Id  * Fee Amount  $* is required for this activity

例:
This Fee Name :  STATE TITLE FEE  Fee Id  2 Fee Amount  $5.50 is required for this activity

我想使用PHP进行的操作是传递字符串并获取结果
  • STATE TITLE FEE
  • 2
  • 5.50

  • 我很确定preg_match_all是我想要的,但是无法弄清楚如何正确使用正则表达式。

    最佳答案

    请尝试以下操作:

    $a = 'This Fee Name :  STATE TITLE FEE  Fee Id  2 Fee Amount  $5.50 is required for this activity';
    $regex = '/This Fee Name :  (.+)  Fee Id  (.+) Fee Amount  \$(.+) is required for this activity/';
    $matches = array();
    preg_match($regex, $a, $matches);
    var_dump($matches);
    

    输出:
    array(4) {
      [0]=>
      string(91) "This Fee Name :  STATE TITLE FEE  Fee Id  2 Fee Amount  $5.50 is required for this activity"
      [1]=>
      string(15) "STATE TITLE FEE"
      [2]=>
      string(1) "2"
      [3]=>
      string(4) "5.50"
    }
    

    10-08 13:06