【问题标题】:Matching data inside a double quote using preg_match_all使用 preg_match_all 匹配双引号内的数据
【发布时间】:2014-05-25 12:22:34
【问题描述】:

下面给出的数据格式为 "name":"Value", "name2":"Value 2", "name3":"Value 3"

$datadump = '"Waived":"Waived", "Until":" until", "HeaderBanInfo":"Ban Info", "StatusLabel":"Current Status:", "StatusBanned":"BANNED", "StatusWarned":"WARNED", "StatusSuspended":"SUSPENDED", "StatusActive":"ACTIVE"';

我试图使用 php 提取 名称

    $pattern = '/"(.*?)":/si';
preg_match_all($pattern, $datadump, $output);

但它不起作用。 希望知道修复它的人会帮助我。 谢谢。

【问题讨论】:

  • $pattern = '/\"([^\"]+)\"\:/';

标签: php json preg-match-all data-extraction


【解决方案1】:

这可能对你有帮助(我不确定你想要输出哪种格式)

$datadump = '"Waived":"Waived", "Until":" until", "HeaderBanInfo":"Ban Info",      "StatusLabel":"Current Status:", "StatusBanned":"BANNED", "StatusWarned":"WARNED", "StatusSuspended":"SUSPENDED", "StatusActive":"ACTIVE"';

print_r(explode(':',str_replace(',',':',$datadump)))

输出:

[0] => "Waived"
[1] => "Waived"
[2] =>  "Until"
[3] => " until"
[4] =>  "HeaderBanInfo"
[5] => "Ban Info"
[6] =>       "StatusLabel"
[7] => "Current Status
[8] => "
[9] =>  "StatusBanned"
[10] => "BANNED"
[11] =>  "StatusWarned"
[12] => "WARNED"
[13] =>  "StatusSuspended"
[14] => "SUSPENDED"
[15] =>  "StatusActive"
[16] => "ACTIVE"

【讨论】:

  • 我只需要名字
【解决方案2】:

这样可以正常工作:

$datadump = '"Waived":"Waived", "Until":" until", "HeaderBanInfo":"Ban Info",      "StatusLabel":"Current Status:", "StatusBanned":"BANNED", "StatusWarned":"WARNED", "StatusSuspended":"SUSPENDED", "StatusActive":"ACTIVE"';
$pattern = '/\"([a-zA-Z\s]*?)\":/si';
preg_match_all($pattern, $datadump, $output);
$names = $output[1];
echo $names[2]; /* this is a simple test */

编辑(仅限字母数字名称)

$datadump = '"a0":"Waived", "Until":" until", "HeaderBanInfo":"Ban Info",      "StatusLabel":"Current Status:", "StatusBanned":"BANNED", "StatusWarned":"WARNED", "StatusSuspended":"SUSPENDED", "StatusActive":"ACTIVE"';
$pattern = '/\"([a-zA-Z0-9]*?)\":/si';
preg_match_all($pattern, $datadump, $output);
$names = $output[1];
for($i=0; $i<sizeof($names); $i++)
   echo $names[$i] . "\n";

EDIT2(它允许名称和冒号之间有空格)

$datadump = '"a0" : "Waived", "Until":" until", "HeaderBanInfo":"Ban Info",      "StatusLabel":"Current Status:", "StatusBanned":"BANNED", "StatusWarned":"WARNED", "StatusSuspended":"SUSPENDED", "StatusActive":"ACTIVE"';
$pattern = '/\"([a-zA-Z0-9]*?)\"[\s]*:/';
preg_match_all($pattern, $datadump, $output);
$names = $output[1];
for($i=0; $i<sizeof($names); $i++)
   echo $names[$i] . "\n";

【讨论】:

  • 我也想知道。当我直接运行代码时,它可以工作,但是当 $datadump 更改时它会失败。 $datadump = file_get_contents($datafile);
  • 好吧,你必须说一下“名字”是什么意思。在我的示例中,名称只能包含小写或大写字母或空格。如果您的姓名包含例如数字,则无法正常工作!
  • "name" 将是一个没有空格的字母数字字符串
  • 变量$datadump的格式和上面写的一样吗?
  • 我已经更新了“EDIT2”下的解决方案,请给我更详细的错误反馈!
【解决方案3】:

试试这个 $pattern = '/\"([a-zA-Z0-9]*?)\":/si';

【讨论】:

    【解决方案4】:

    好的,因为从您的问题中不清楚您想要哪个名称。下面的代码显示了如何获取任何名称:

    获取名字:

    $datadump = '"Waived":"Waived", "Until":" until", "HeaderBanInfo":"Ban Info", "StatusLabel":"Current Status:", "StatusBanned":"BANNED", "StatusWarned":"WARNED", "StatusSuspended":"SUSPENDED", "StatusActive":"ACTIVE"';
    
    $pattern_for_1st_name = '/"([a-zA-Z:\s]*?)":/';
    
    preg_match_all($pattern_for_1st_name, $datadump, $output);
    
    var_dump($output[1]);
    

    这将输出对中的名字:

    array(8) { [0]=> string(6) "Waived" [1]=> string(5) "Until" [2]=> string(13) "HeaderBanInfo" [3]=> string(11) "StatusLabel" [4]=> string(12) "StatusBanned" [5]=> string(12) "StatusWarned" [6]=> string(15) "StatusSuspended" [7]=> string(12) "StatusActive" } 
    

    获取第二个名字:

    现在,如果你想要这对中的第二个名字,下面是它的模式:

     $pattern_for_2nd_name = '/:"([a-zA-Z:\s]*?)"/';
    
     preg_match_all($pattern_for_2nd_name, $datadump, $output);
    
     var_dump($output[1]);
    

    这将输出第二个名字:

    array(8) { [0]=> string(6) "Waived" [1]=> string(6) " until" [2]=> string(8) "Ban Info" [3]=> string(15) "Current Status:" [4]=> string(6) "BANNED" [5]=> string(6) "WARNED" [6]=> string(9) "SUSPENDED" [7]=> string(6) "ACTIVE" }
    

    我不知道您的数据是否实际上包含 Current Status: 字或冒号是拼写错误,因此也包含匹配的冒号。

    【讨论】:

      【解决方案5】:

      由于你的输入字符串是JSON,你不需要使用preg_match_all,最好使用你的数据格式:

      print_r(array_keys(json_decode('{' . $datadump . '}', true)));
      

      PHP 手册:
      json_decode
      array_keys

      关于您的模式:
      它不起作用,因为.*? 允许任何类型的字符,包括"。正则表达式引擎将从左到右逐个字符地处理字符串,并尽快给出匹配。这就是为什么您的第二个结果是:"Waived", "Until":

      为了避免这些结果,一种方法是禁止" 使用否定字符类代替点:
      /"([^"]+)":/ (请注意,使用这个,不再需要惰性量词.)

      【讨论】:

        【解决方案6】:

        查看我的solution

        json_decode('{'.$datadump.'}');
        

        哈哈

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-05-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多