We help IT Professionals succeed at work.

Converting this string to an array

Alex Lord
Alex Lord asked
on
89 Views
Last Modified: 2018-08-30
<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

Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Commented:
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

CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Chris example worked the best, Julian ur one didn't agree with my set-up but a good learning curve for potential projects.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.