douxin8383 2014-05-10 15:29
浏览 33
已采纳

正则表达式PHP尝试剖析字符串

Here is an example of my string:

$string = 'Vivamus non risus non diam {some-text-here} varius sollicitudin in non dui. Phasellus egestas {some-more-text-here} quis augue eu pharetra. Curabitur rutrum non lectus et aliquam. Fusce quis rutrum magna. Sed at tristique dui, scelerisque tincidunt tortor. Integer a nibh gravida urna pulvinar vehicula. Curabitur ut felis ut mi imperdiet condimentum {and-some-goes-here-too}. Vestibulum sit amet {of-course-here-as-well} diam mi. Quisque diam dui, convallis dapibus suscipit sit amet, auctor in turpis.';

If you notice there are {these-things-with-text-in-them} peppered throughout the $string. I use this function to pull them out of the $string.

preg_match_all('/\{(.*?)\}/', $string, $bracket_data);
print '<pre>'; print_r($bracket_data[1]); print '</pre>';

Which will spit out this:

Array
(
    [0] => some-text-here
    [1] => some-more-text-here
    [2] => and-some-goes-here-too
    [3] => of-course-here-as-well
)

That does the trick nicely. What I'd like to have is the same result, but with the parts of the $string that are not within brackets, still using preg_match_all(). So in this case using $string as an example, the desired result would be:

Array
(
    [0] => Vivamus non risus non diam 
    [1] =>  varius sollicitudin in non dui. Phasellus egestas 
    [2] =>  quis augue eu pharetra. Curabitur rutrum non lectus et aliquam. Fusce quis rutrum magna. Sed at tristique dui, scelerisque tincidunt tortor. Integer a nibh gravida urna pulvinar vehicula. Curabitur ut felis ut mi imperdiet condimentum 
    [3] => . Vestibulum sit amet 
    [4] =>  diam mi. Quisque diam dui, convallis dapibus suscipit sit amet, auctor in turpis.
)

Does anyone have any suggestions, or critique for the existing regex I have?

NOTE: The text within the brackets can be found in any arrangement. They can be at the beginning, shoulder-to-shoulder, at the end, and any amount. Same for the text outside of the brackets

  • 写回答

2条回答 默认 最新

  • dongliu1883 2014-05-10 15:33
    关注

    Use the same regex with preg_split(). I've also added \s* around the regex to exclude whitespace from the matches.

    $arr = preg_split('/\s*\{(.*?)\}\s*/', $string);
    

    Output:

    Array
    (
        [0] => Vivamus non risus non diam
        [1] => varius sollicitudin in non dui. Phasellus egestas
        [2] => quis augue eu pharetra. Curabitur rutrum non lectus et aliquam. Fusce quis rutrum magna. Sed at tristique dui, scelerisque tincidunt tortor. Integer a nibh gravida urna pulvinar vehicula. Curabitur ut felis ut mi imperdiet condimentum
        [3] => . Vestibulum sit amet
        [4] => diam mi. Quisque diam dui, convallis dapibus suscipit sit amet, auctor in turpis.
    )
    

    Demo

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料