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 8269213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:08:51+00:00 2026-06-08T06:08:51+00:00

The article segmentation have two kinds of cases: 1. < p > the first

  • 0

The article segmentation have two kinds of cases:


 1. < p > the first paragraph < / p > < p > the second paragraph < / p >...
 2. < p > the first period of < br / > < br / > the second paragraph < br / > < br / > the third paragraph < / p >

I write the code as follows:


$body_arr = preg_split('/\<\/?p\>/',$body,-1,PREG_SPLIT_NO_EMPTY);
echo count($body_arr);
    if(count($body_arr)<4) 
    {
       $body_arr = preg_split('/(\<br\/?\>)\s*\\1/',$body,-1,PREG_SPLIT_NO_EMPTY);
       $body1 = $body2 = $body3 = '';
       $total = count($body_arr);
       $maxed = max(floor($total / 2), 3);
       foreach ($body_arr as $k => $v) 
       {
            if ($k == 0) 
            {
                $body1 = $v . "<br><br>";
            } 
            else if ($k < $maxed) 
            {
                $body2.=$v . "<br><br>";
            } 
            else 
            {
                $body3.=$v . "<br><br>"  ;
            }
       }
     }
  • It is the second

  • The result is wrong.

  • 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-06-08T06:08:54+00:00Added an answer on June 8, 2026 at 6:08 am

    You can split the text with a single regex using nested groups. You’re starting with a p tag, followed by multiple paragraphs that end in either another close/open p tag, a pair of br tags, or a final close p tag.

    The close/open p tag can be represented with the following:

    <\s*//*\s*p\s*>[\s|\r|\n]*<\s*p\s*>
    

    The double br tag can be represented with the following:

    <\s*br\s*//*\s*>[\s|\r|\n]*<\s*br\s*//*\s*>
    

    And the close p tag can be represented with the following:

    <\s*//*\s*p\s*>
    

    Note that I’m allowing for space between tags because you had it in your example, but remove the \s* if they’re not necessary. Stitch that together using some nested groups and you end up with something like this:

    <\s*p\s*>((?<Paragraph>[^<]*)((<\s*//*\s*p\s*>[\s|\r|\n]*<\s*p\s*>)|(<\s*br\s*//*\s*>[\s|\r|\n]*<\s*br\s*//*\s*>)|(<\s*//*\s*p\s*>)))*
    

    I tested that with your examples and it works. From the example I’m assuming that you don’t have tags in the middle of the paragraphs, but you’ll have to use something fancier than not the start of a tag to capture the actual text if that isn’t the case.

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

Sidebar

Related Questions

one Article has many Comments, and i want to fetch all Articles that have
I have an article based website where users can login, post articles etc. The
I have three models Article , Author , and AuthorLine presenting relationship between article
I saw an article here: http://code.lancepollard.com/automatically-publish-posts-to-stumbleupon-with-ruby I don't know Ruby, but the following lines
Actually, I have two related questions. I'm capturing filtered network traffic by libpcap on
This article about Java security says: Code in the Java library consults the Security
/news/article-title.html is not being caught by the regex: ^/news/[^(archives)].+.html ? I'm trying to have
This article on Binary-compatible C++ Interfaces contains the code: class Window { public: //
The code below is from the well-known article Smashing The Stack For Fun And
Each Article can have unlimited categories. Categories are saved in the database like this

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.