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.
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:
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:
Here’s how to deal with the above and get an array that has the number in order:
Dealing with nesting
Try it out here