doubi1797 2014-02-19 13:00
浏览 96
已采纳

PHP(正则表达式)特殊的未知单词。 字符“对匹配”

I don't know, how to make the subject (title) clear enough to understand. I've got something like this:

<h2>Title</h2>
<<navigation id="submenu">>
    <<main level="1" asd="2">>
    <<main level="1" asd="2">>
    <<notmain>>asd<</notmain>>
<</navigation>>
<p><a href="..">asd</a>asdasdasd</p>
Oh no! The great rabbit is attacking us, and we are 
only knights of a square table!
<h2>Here another tag can occur</h2>
<<footer>>
    <<copyright id="copy">>
<</footer>>

I must find and remember (as text) objects that have two tags not one (but only the main parent). So for this example I need to output something like this:

array(
    0 => '<<navigation id="submenu">><<main level="1" asd="2">><<main level="1" asd="2">><<notmain>>asd<</notmain>><</navigation>>',
    1 => '<<footer>><<copyright id="copy">><</footer>>';

Spaces and white spaces, and tabs, and rest doesn't matter, as it is easy to strip it by using trim, and str_replace. The only problem is searching method.

I was trying to regex this but there are few problems.

  1. I'm interested in parents only. So no recursive searching inside, just outside element with '<<' '>>'and all the inside elements (don't care how they look).
  2. I haven't got any data of how the first word looks. It can be <>. And then return all :). I don't know, and haven't found any solution if regex can remember what it has found.

I hope that my question is clear.

I know PHP (quite well) so text solution without code, or idea will be also helpful.

If there exists a solution without regex, it will be great. There is of course a bruteforce solution, (character after character analyzing) but it needs tons of code...

  • 写回答

1条回答

  • doutou7961 2014-02-19 13:16
    关注

    How about this one:

    %^<<([^<]+?)>>$(.+?)^<<([^<]+?)>>%sm

    Gives this result when used with preg_match_all

    array (
      0 => 
      array (
        0 => '<<navigation id="submenu">>
        <<main level="1" asd="2">>
        <<main level="1" asd="2">>
        <<notmain>>asd<</notmain>>
    <</navigation>>',
        1 => '<<footer>>
        <<copyright id="copy">>
    <</footer>>',
      ),
      1 => 
      array (
        0 => 'navigation id="submenu"',
        1 => 'footer',
      ),
      2 => 
      array (
        0 => '
        <<main level="1" asd="2">>
        <<main level="1" asd="2">>
        <<notmain>>asd<</notmain>>
    ',
        1 => '
        <<copyright id="copy">>
    ',
      ),
      3 => 
      array (
        0 => '/navigation',
        1 => '/footer',
      ),
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料