how to extract content from <div> tag using php

Suppose I have  content like this

<div>content  paragraph 1</div>
<div>content, paragraph 2</div>
<div>some content, paragraph 3</div>

separates and get  contents of each  <div> tag   and output will display in the form of array

Array
(
[0] => Array   //paragraph 1
(
)
[1] => Array    //paragraph 2
(
)
[2] => Array    //paragraph 3
(
)
)
prashanth agsoftware engineer Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
I can answer this question with a regular expression, but you're on thin ice if you think you can use regular expressions to parse HTML documents.  In a humorous way, this explains why:
http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags?answertab=oldest#tab-top

The problem you will find is that HTML is self-aware and stateful, so regular expressions are not powerful enough to do the job except in very limited test cases.  In practice, what passes for HTML, even for valid HTML, is beyond the ability of REGEX.  You need a state engine or dom parser to get dependable results.
0
Ray PaseurCommented:
<?php // demo/temp_prasan.php

/**
 * http://www.experts-exchange.com/questions/28697375/how-to-extract-content-from-div-tag-using-php.html
 *
 * http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags?answertab=oldest#tab-top
 */
error_reporting(E_ALL);

$htm = <<<EOD
<div>content  paragraph 1</div>
<div>content, paragraph 2</div>
<div>some content, paragraph 3</div>
EOD;

$rgx
= '#'            // REGEX DELIMITER
. preg_quote('<div>')
. '('            // START CAPTURE GROUP
. '.*?'          // ANYTHING OR NOTHING
. ')'            // END CAPTURE GROUP
. preg_quote('</div>')
. '#'            // REGEX DELIMITER
. 'i'            // FLAG - CASE-INSENSITIVE
;

preg_match_all($rgx, $htm, $mat);

echo '<pre>';
print_r($mat[1]);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
prashanth agsoftware engineer Author Commented:
thank u Paseur
0
prashanth agsoftware engineer Author Commented:
i made small change
$data = file_get_contents('http://localhost/test/div.html');
$regx= '#<div>(.*?)</div>#i';  
preg_match_all($regx, $data, $mat);
print_r($mat[1]);
0
Ray PaseurCommented:
There is usually a reason why I include a function call in one of my PHP scripts.  Here is the function you omitted.  When you read the man page, you will probably understand why I included it.
http://php.net/manual/en/function.preg-quote.php
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.