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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:18:47+00:00 2026-05-24T20:18:47+00:00

I need to take a string of html text like: <p>This is a line

  • 0

I need to take a string of html text like:

<p>This is a line with no spans<br>
This is a line <span class="second">This is secondary</span><br>  
This is another line <span class="third">And this is third</span> <span class="four">this is four</span></p>

And have it end up as an array in PHP like:

array(
    "This is a line with no spans",
    array(
      "This is a line",
      second => "This is secondary",
    ),
    array(
      "This is another line",
      third => "And this is third",
      four => "this is four"
    )
);

Getting each line into it’s own value was easy, I just split the text on <br> and that works fine, but getting lines to be split with the class name I can’t quite get. I feel like php’s preg_split may hold the key, but I kind of suck with regular expressions and I can’t get it figured out.

Any ideas?

  • 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-24T20:18:48+00:00Added an answer on May 24, 2026 at 8:18 pm

    It’s not a good idea to use regular expressions to parse HTML (cite). It’s just not a suitable tool; see @JAAulde’s answer.

    The best way is to do it purely with the DOM. Loop through all child nodes (including text nodes) to format the array the way you want. Like this:

    $p = // get paragraph tag...
    $lines = array();
    $pChildren = $p->childNodes;
    for ($i = 0; $i < $pChildren->length; $i++) {
        $line = array();
        $child = $pChildren->item($i);
        if ($child instanceof DOMText) {
            $line[] = $child->wholeText;
        } elseif ($child instanceof DOMElement) {
            if (strtolower($child->tagName) == 'br') {
                $lines[] = $line;
                $line = array();
            } elseif (strtolower($child->tagName) == 'span' && $child->hasAttribute('class')) {
                $line[$child->getAttribute('class')] = $child->nodeValue;
            }
        }
    }
    

    Warning: treat the above as pseudo-code, it has not been tested at all, just going from experience and the manual.

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

Sidebar

Related Questions

I need to be able to take a string like: '''foo, bar, one, two,
I have the need to take a string argument and create an object of
I need a Regex Statement (run in c#) that will take a string containing
I need to take a paragraph of text and extract from it a list
From my previous question for selecting specific html text, I have gone through this
Is it possible in ASP.NET to take a string containing some HTML and make
Objective: I would like to take a JSON String and pass it into JavaScript
I need an html helper that would take care of tabs functionality on a
I need to take text from a source as plain text and display it
In a new class I'm writing, I need to take the contents of an

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.