1

I want to parse data in between brackets. Below is the code where you can see what I'm doing. I would also like to avoid using XML.

$query = '
    [page:1]
          <html>
                all the html
          </html>
    [/page:1]


    [page:2]
          <html>
                all the html
          </html>
    [/page:2]
';

I want to create a loop script that will use regex to find all instances of [page:x]; which in the example above is 2. And then with a get function we can specify the page we want.

if(isset($_GET['page'])) {

      $page = $_GET['page'];
      $regex = '\\['page':(.*?)\\';

      echo preg_match($regex, $query);

}

Any thoughts?

4
  • 1
    my thought is that it isn't clear what your question is. Please clarify the question title and description. Commented Feb 14, 2011 at 1:45
  • Aren't you getting a syntax error when trying to run this? Commented Feb 14, 2011 at 1:45
  • url.php?page=1 would pull content from [page:1]mycontent[/page1] and the output would be "mycontent". or for url.php?page=2 it would pull content in between [page:2] second page stuff[/page2] ouput would be "second page stuff". It has to be dynamic though, because any database entry could have any amount of page brackets ([page:x]) Commented Feb 14, 2011 at 1:52
  • Can't you store the pages in a saner manner in the database? Commented Feb 14, 2011 at 1:58

1 Answer 1

2

This should find all the matching blocks at once:

preg_match_all('/\[page:([0-9]+)\](.+?)\[\/page:$1\]/', $page, $matches)

I strongly doubt regex is the most suitable solution for what you're trying to accomplish though.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.