Converting this string to an array

<div id='property-2565' class=' col-md-4'><label style='float:left;' class ='control-label' for='2565'>textline2</label><input class='form-control' type='text' name='property-2565' id='property-2565' value='test1'/></div><div id='property-2561' class=' col-md-4'><label style='float:left;' class ='control-label' for='2561'>select1</label><select style ='width:100%;' class='form-control' name='property'2561'' id='property'2561''><option value=''>- select -</option><option value='1' >1</option><option value='2' >2</option><option value='3' >3</option></select></div><div id='property-2555' class=' col-md-4'><label style='float:left;' class ='control-label' for='2555'>textline1</label><input class='form-control' type='text' name='property-2555' id='property-2555' value='test3'/></div>

Open in new window


so here is my lovely string which is exactly 3 separate divs with two inputs and a select.

my question is it possible or how can i transfer this string into an array to display these 3 divs separately as a array.


like so

0 => <div id='property-2565' class=' col-md-4'><label style='float:left;' class ='control-label' for='2565'>textline2</label><input class='form-control' type='text' name='property-2565' id='property-2565' value='test1'/>
1 => <div id='property-2561' class=' col-md-4'><label style='float:left;' class ='control-label' for='2561'>select1</label><select style ='width:100%;' class='form-control' name='property'2561'' id='property'2561''><option value=''>- select -</option><option value='1' >1</option><option value='2' >2</option><option value='3' >3</option></select></div>
2 => <div id='property-2555' class=' col-md-4'><label style='float:left;' class ='control-label' for='2555'>textline1</label><input class='form-control' type='text' name='property-2555' id='property-2555' value='test3'/></div> 

Open in new window

Alex LordAsked:
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.

Julian HansenCommented:
You can use preg_match_all()
<?php
$input = "<div id='property-2565' class=' col-md-4'><label style='float:left;' class ='control-label' for='2565'>textline2</label><input class='form-control' type='text' name='property-2565' id='property-2565' value='test1'/></div><div id='property-2561' class=' col-md-4'><label style='float:left;' class ='control-label' for='2561'>select1</label><select style ='width:100%;' class='form-control' name='property'2561'' id='property'2561''><option value=''>- select -</option><option value='1' >1</option><option value='2' >2</option><option value='3' >3</option></select></div><div id='property-2555' class=' col-md-4'><label style='float:left;' class ='control-label' for='2555'>textline1</label><input class='form-control' type='text' name='property-2555' id='property-2555' value='test3'/></div>";
$result = preg_match_all("|(<div.*</div\>)|U", $input, $match);

echo "{$result} matches\n";
if ($result > 0) {
    var_dump($match[1]);
}

Open in new window

Output
3 matches
array(3) {
  [0]=>
  string(221) "<div id='property-2565' class=' col-md-4'><label style='float:left;' class ='control-label' for='2565'>textline2</label><input class='form-control' type='text' name='property-2565' id='property-2565' value='test1'/></div>"
  [1]=>
  string(348) "<div id='property-2561' class=' col-md-4'><label style='float:left;' class ='control-label' for='2561'>select1</label><select style ='width:100%;' class='form-control' name='property'2561'' id='property'2561''><option value=''>- select -</option><option value='1' >1</option><option value='2' >2</option><option value='3' >3</option></select></div>"
  [2]=>
  string(221) "<div id='property-2555' class=' col-md-4'><label style='float:left;' class ='control-label' for='2555'>textline1</label><input class='form-control' type='text' name='property-2555' id='property-2555' value='test3'/></div>"
}

Open in new window

0
Chris StanyonWebDevCommented:
You can use the DomDocument class. Something like this:

$html = "<div id='property-2565' class=' col-md-4'><label style='float:left;' class ='control-label' for='2565'>textline2</label><input class='form-control' type='text' name='property-2565' id='property-2565' value='test1'/></div><div id='property-2561' class=' col-md-4'><label style='float:left;' class ='control-label' for='2561'>select1</label><select style ='width:100%;' class='form-control' name='property'2561'' id='property'2561''><option value=''>- select -</option><option value='1' >1</option><option value='2' >2</option><option value='3' >3</option></select></div><div id='property-2555' class=' col-md-4'><label style='float:left;' class ='control-label' for='2555'>textline1</label><input class='form-control' type='text' name='property-2555' id='property-2555' value='test3'/></div>";

$dom = new DOMDocument();
$dom->loadHTML($html);

foreach ($dom->getElementsByTagName('div') as $div):
    $divArray[] = $dom->saveHTML($div);
endforeach;

var_dump($divArray);

Open in new window

You are likely to get some warning because your HTML is not valid. It will still work but you might not get out what you expect. You have duplicate IDs and you have some errors with your quotes:

name='property'2561'' id='property'2561''

Also, the 'for' property of a label needs to point to an ID, so:

<label for='2565'>

should probably be :

<label for='property-2565'>
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
Alex LordAuthor Commented:
Chris example worked the best, Julian ur one didn't agree with my set-up but a good learning curve for potential projects.
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
arrays

From novice to tech pro — start learning today.