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 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用