I know we can use PHP DOM to parse HTML using PHP. I found lot of questions here on stackoverflow too. But I have a specific requirement. I have an HTML content like below
<p class="Heading1-P">
<span class="Heading1-H">Chapter 1</span>
</p>
<p class="Normal-P">
<span class="Normal-H">This is chapter 1</span>
</p>
<p class="Heading1-P">
<span class="Heading1-H">Chapter 2</span>
</p>
<p class="Normal-P">
<span class="Normal-H">This is chapter 2</span>
</p>
<p class="Heading1-P">
<span class="Heading1-H">Chapter 3</span>
</p>
<p class="Normal-P">
<span class="Normal-H">This is chapter 3</span>
</p>
I want to parse the above HTML and save the conent into two different array like
$heading
and $content
$heading = array('Chapter 1','Chapter 2','Chapter 3');
$content = array('This is chapter 1','This is chapter 2','This is chapter 3');
I can achieve this simply using jQuery. But I am not sure, is it the right way. It would be great if some can point me to right direction. Thanks in advance.
Try to look at PHP Simple HTML DOM Parser
It has brilliant syntax similar to jQuery so you can easily select any element you want by ID or class
// Create DOM from URL or file
// Find all images
// Find all links
I have used domdocument and domxpath to get the solution, you can find it at:
Live result: http://saji89.codepad.org/2TyOAibZ
One option for you is to use DOMDocument and DOMXPath. They do require a bit of a curve to learn, but once you do, you will be pretty happy with what you can achieve.
Read the following in php.net
http://php.net/manual/en/class.domdocument.php
http://php.net/manual/en/class.domxpath.php
Hope this helps.