3

Probably easy to do but I can't seem to generate the correct regex.

Say I have this string

$string = '<h2>Header 1</h2><p>ahs da sdka dshk asd haks</p><img src="http://dummyimage.com/100x100/" width="100" height="100" alt="Alt" /><h2>Header 2</h2><p>asdkhas daksdha kd ahs</p><em>Lame</em><p>trhkbasd akhsd ka dkhas</p><h2>Header 3</h2><p>ajdas ahkds hakd</p>';

And I need it like this

$array[0] = '<h2>Header 1</h2><p>ahs da sdka dshk asd haks</p><img src="http://dummyimage.com/100x100/" width="100" height="100" alt="Alt" />';
$array[1] = '<h2>Header 2</h2><p>asdkhas daksdha kd ahs</p><em>Lame</em><p>trhkbasd akhsd ka dkhas</p>';
$array[2] = '<h2>Header 3</h2><p>ajdas ahkds hakd</p>';

...and so on if my string contains more of those H2 blocks.

So, the split-point is at H2 and it needs to keep the HTML-tags. Any pointers?

3 Answers 3

4

Use preg_split() with a positive lookahead for the opening tag:

print_r(preg_split('/(?=<h2>)/', $string, -1, PREG_SPLIT_NO_EMPTY));

The positive lookahead simply tells the regex parser to split text surrounding <h2>, but not eliminate the tag. If you split by /<h2>/, the tag disappears, just like if you split with explode().

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

1 Comment

This did the trick. And I was on PHP 5.3.0> so no split() for me.
1
$result = split('(?=<h2>)', $string);

or

$result = preg_split('/(?=<h2>)/', $string);

1 Comment

split() is deprecated in favor of preg_split(). But this is still a valid answer anyway.
0
$string = '<h2>Header 1</h2><p>ahs da sdka dshk asd haks</p><img src="http://dummyimage.com/100x100/" width="100" height="100" alt="Alt" /><h2>Header 2</h2><p>asdkhas daksdha kd ahs</p><em>Lame</em><p>trhkbasd akhsd ka dkhas</p><h2>Header 3</h2><p>ajdas ahkds hakd</p>';

$matches    = split('<h2>', $string);

print_r($matches);

This is deprecated as of PHP 5.3.0 though.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.