Your question, your audience. Choose who sees your identity—and your question—with question security.
<?php
/**
* See http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28607748.html
*/
error_reporting(E_ALL);
// TEST DATA CREATED USING NOWDOC SYNTAX
$str = <<<'EOD'
<html>
<body>
<div> various text </div>
<div class="container">
<h3 class="main">title here</h3> //text in bold would be $main[0]
<div> various text here</div>
<div class="listing">more text here</div> //text in bold would be $listing[0]
<div> various other text here</div>
<span class "wrap">wrap up text</span> //text in bold would be $wrap[0]
<div>end of this listing</div>
</div>
<div class="container">
<h3 class="main">title 2 here</h3> //text in bold would be $main[1]
<div> various text here</div>
<div class="listing">more text here 2</div> //text in bold would be $listing[1]
<div> various other text here</div>
<span class "wrap">wrap up text 2</span> //text in bold would be $wrap[1]
<div>end of this listing</div>
</div>
<div class="container">
<h3 class="main">title 3 here</h3> //text in bold would be $main[2]
<div> various text here</div>
<div class="listing">more text here 3</div> //text in bold would be $listing[2]
<div> various other text here</div>
<span class "wrap">wrap up text 3</span> //text in bold would be $wrap[2]
<div>end of this listing</div>
</div>
<div> various text</div>
</body>
</html>
EOD;
// THE SIGNAL INFORMATION
$trap['main'] = [ '<h3 class="main">', '</h3>' ];
$trap['listing'] = [ '<div class="listing">', '</div>' ];
$trap['wrap'] = [ '<span class "wrap">', '</span>' ];
// EXTRACT THE DATA AND BUILD NEW ARRAYS
foreach ($trap as $var => $arr)
{
$rgx = '#' . '(' . preg_quote($arr[0]) . ')(.*?)(' . preg_quote($arr[1]) . ')#';
preg_match_all($rgx, $str, $mat);
$$var = $mat[2];
}
// SHOW THE WORK PRODUCTS
$kount = 0;
while ($kount > -1)
{
echo '<p>';
echo $main[$kount] . '<br />';
echo $listing[$kount] . '<br />';
echo $wrap[$kount] . '<br />';
echo '</p>' . PHP_EOL;
$kount++;
if (empty($main[$kount])) break;
}
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.