开发者

Parsing data with REGEX (PHP)

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 = '\\['pa开发者_StackOverflow中文版ge':(.*?)\\';

      echo preg_match($regex, $query);

}

Any thoughts?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜