开发者

Need a little reg-exp help

开发者 https://www.devze.com 2023-03-05 04:36 出处:网络
I\'m horribl开发者_如何学Ce at reg-exp\'s, so I need a little help getting it right. I got a PHP-variable containing HTML-code created from a PDF. The pages are separate with a comment in the followi

I'm horribl开发者_如何学Ce at reg-exp's, so I need a little help getting it right.

I got a PHP-variable containing HTML-code created from a PDF. The pages are separate with a comment in the following style:

<!-- Page 1 -->

What I need is to split the content on each of these comment so I can separate the pages from eachother. I tried using just explode('<!-- Page', $content), but that leaves the rest of the tag on the start of the next page so obviously it's not a good enough solution.

So what I need help with is finding a reg-exp that will split on <!-- Page X --> where the X can be any number. Anyone able to help me out?


This seems to work just fine;

<?php

$foo = '

Hello.

<!-- Page 2 -->

Bar

<!-- Page 3 -->

Foo
';

$pages = array_map( 'trim', preg_split( '~<!-- Page (\d+) -->~m', $foo ) );

var_dump( $pages );

Output:

berry@berry-pc:~$ php foo.php 
    array(3) {
  [0]=>
  string(6) "Hello."
  [1]=>
  string(3) "Bar"
  [2]=>
  string(3) "Foo"
}

Hope that helps.


simple regex:

<!-- Page [0-9]+ -->
0

精彩评论

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