1

我尝试了很多东西,但没有任何效果。我有一个字符串,我想用逗号分割字符串,但如果逗号在括号大括号引号或单引号,之间,则不要分割- 对于这种特定情况,以维护完整的 iframe(即摧毁一切)字符串。下面是字符串...

class : putmeincoach, 
 id : random_id,  
   responsive : {   [ type= text ;  html = "< iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&ie=UTF8&ll=34.009394,-118.488514&spn=0.020687,0.020921&t=m&z=16"> < /iframe >" ;  ]  }

我只是想

Array (
   [0] = 'class : putmeincoach',
   [1] = 'id : random_id',
   [2] = 'responsive : {   [ type= text ;  html = "< iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&ie=UTF8&ll=34.009394,-118.488514&spn=0.020687,0.020921&t=m&z=16"> < /iframe >" ;  ]  } '

我可以使用 preg_split 或 preg_match 和一些通用的正则表达式模式来做到这一点吗?Lemme 还提到双引号之间没有逗号真的是最重要的

4

2 回答 2

1

在 PHP 中:

<?php
$input = '
    class : putmeincoach, 
    id : random_id, 
    responsive : {   [ type= text ;  html = "&lt; iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&amp;ie=UTF8&amp;ll=34.009394,-118.488514&amp;spn=0.020687,0.020921&amp;t=m&amp;z=16"&gt; &lt; /iframe &gt;" ;  ]  }, 
    testsinglequotes: \'hello world\', 
    testdoublequotes: "hello hello"';
$pattern = '/ 
    \b(\w+)\s*:   # capture the word before the colon 
    \s*(          # start capture group match after the colon
        {[^}]*}\s*|     # match everything between braces  OR
        "[^"]*"\s*|      # match everything between double quotes OR
        \'[^\']*\'\s*|   # match everything between single OR
        [^,]*           # match everything up to the comma 
        )         # end capture group
    (?:,|$)       # match comma or end of string (non-capture group) 
    /x';
preg_match_all($pattern ,$input, $matches);
print_r($matches);
?>

输出 PHP 5.3.13:

Array
(
    [0] => Array
        (
            [0] => class : putmeincoach,
            [1] => id : random_id,
            [2] => responsive : {   [ type= text ;  html = "&lt; iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&amp;ie=UTF8&amp;ll=34.009394,-118.488514&amp;spn=0.020687,0.020921&amp;t=m&amp;z=16"&gt; &lt; /iframe &gt;" ;  ]  },
            [3] => testsinglequotes: 'hello world',
            [4] => testdoublequotes: "hello hello"
        )

    [1] => Array
        (
            [0] => class
            [1] => id
            [2] => responsive
            [3] => testsinglequotes
            [4] => testdoublequotes
        )

    [2] => Array
        (
            [0] => putmeincoach
            [1] => random_id
            [2] => {   [ type= text ;  html = "&lt; iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&amp;ie=UTF8&amp;ll=34.009394,-118.488514&amp;spn=0.020687,0.020921&amp;t=m&amp;z=16"&gt; &lt; /iframe &gt;" ;  ]  }
            [3] => 'hello world'
            [4] => "hello hello"
        )

)

正则表达式是你的朋友

于 2013-03-08T19:15:20.550 回答
0

我试过

^(class.*?,).*?(id.*?,).*?(responsive.*,?)$

使用http://www.gskinner.com/RegExr/这有效。棘手的事情是对前两组中的 .* 和最后一组中的 ',' 使用惰性匹配。

我认为,您需要将其适应 PHP,因为正则表达式处理有点不同。

于 2013-03-08T19:20:06.167 回答