Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1101223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T01:00:14+00:00 2026-05-17T01:00:14+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T01:00:15+00:00Added an answer on May 17, 2026 at 1:00 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This probably is easy but I cant seem to get lets say I have
This is probably really easy but I can't seem to figure out how to
This is probably easy, but sadly I can't really find the answer. Let's say
This is probably very easy to do but for some reason I can't seem
Ok so this i probably very easy but i can't seem to figure it
This is probably an easy question but I can't find the answer... I have
This is probably a really easy one, but I can't seem to find the
This is probably an easy question but I can't figure out the best answer
This is probably an easy solution but I can't get my head around it.
This is blowing my mind because it's probably an easy solution, but I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.