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

  • Home
  • SEARCH
  • 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 6181653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:03:51+00:00 2026-05-24T01:03:51+00:00

Here is the string: <div>This is a test.</div> <div>This <b>another</b> a test.</div> <div/> <div>This

  • 0

Here is the string:

<div>This is a test.</div>
<div>This <b>another</b> a test.</div>
<div/>
<div>This is last a test.</div>

I wanna to separate the following string to array like this:

{"This is a test.", "This <b>another</b> a test.", "", "This is last a test."}

Any idea to do so in php? Thank you.

  • 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-24T01:03:52+00:00Added an answer on May 24, 2026 at 1:03 am

    I assume your HTML is malformed on purpose

    There are many options, includin xpath and numerous libraries. Regex is not a good idea. I find DOMDocument fast and relatively simple.

    getElementsByTagName then iterate over them getting the innerHTML.

    Example:

    <?php
    function get_inner_html( $node ) { 
        $innerHTML= ''; 
        $children = $node->childNodes; 
        foreach ($children as $child) { 
            $innerHTML .= $child->ownerDocument->saveXML( $child ); 
        } 
    
        return $innerHTML; 
    }
    $str = <<<'EOD'
    <div>This is a test.</div>
    <div>This <b>another</b> a test.</div>
    <div/>
    <div>This is last a test.</div>
    EOD;
    
    $doc = new DOMDocument();
    $doc->loadHTML($str);
    $ellies = $doc->getElementsByTagName('div');
    foreach ($ellies as $one_el) {
        if ($ih = get_inner_html($one_el))
            $array[] = $ih;
    }
    ?>
    <pre>
    <?php print_r($array); ?>
    </pre>
    
    // Output
    // Note that there would be
    // a 4th array elemnt w/o the `if ($ih = get_inner_html($one_el))` check:
    Array
    (
        [0] => This is a test.
        [1] => This <b>another</b> a test.
        [2] => This is last a test.
    )
    

    Try it out here


    Note:

    The above will work fine as long as you don’t have nested DIVS. If you do have nesting, you have to exclude the nested children as you loop through innerHTML.

    For example let’s say you have this HTML:

    <div>One
        <div>Two</div>
        <div>Three</div>
    <div/>
    <div>Four
        <div>Five</div>
    </div>
    

    Here’s how to deal with the above and get an array that has the number in order:

    Dealing with nesting

    <?php
    function get_inner_html_unnested( $node, $exclude ) { 
        $innerHTML= ''; 
        $children = $node->childNodes; 
        foreach ($children as $child) {     
            if (!property_exists($child, 'tagName') || ($child->tagName != $exclude)) 
                $innerHTML .= trim($child->ownerDocument->saveXML( $child ));
        } 
    
        return $innerHTML; 
    }
    $str = <<<'EOD'
    <div>One
        <div>Two</div>
        <div>Three</div>
    <div/>
    <div>Four
        <div>Five</div>
    </div>
    EOD;
    
    $doc = new DOMDocument();
    $doc->loadHTML($str);
    $ellies = $doc->getElementsByTagName('div');
    foreach ($ellies as $one_el) {
        if ($ih = get_inner_html_unnested($one_el, 'div'))
            $array[] = $ih;
    }
    ?>
    <pre>
    <?php print_r($array); ?>
    </pre>
    

    Try it out here

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

Sidebar

Related Questions

Here's the string: 04046955104021109 I need it to be formatted like so: 040469551-0402-1109 What's
Here is my string $newPath = '/~new/assets/js/../packages/prettyphoto/js/jquery.prettyPhoto.js'; Now check this output var_dump($newPath); // string(64)
I was wondering about the last constructor for std::string mentioned here . It says:
this is inside my sample.html <html> <head> <title>Test</title> </head> <body> <div id=testingID>hello</div> </body> </html>
Possible Duplicate: PHP Strip_tags for div with a specific ID? I have a string
I'm getting an unterminated string literal Javascript error with this code: var test =
Can any one help me with this code: ASP: <script type=text/javascript> function confirm_delete(div.ID)// problem
I am having trouble parsing some returned XML using this command: XML::Parser.string(xml_string).parse Here is
My code-behind looks like this class Image { public string tnImg { get; set;
When I click on the following div: <div id=Result>Click here for the time.</div> I

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.