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...

有帮助吗?

解决方案

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',
  ),
)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top