doubi1797
2014-02-19 13:00 阅读 92
已采纳

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 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',
      ),
    )
    
    点赞 评论 复制链接分享

相关推荐