Avatar of Alex Lord
Alex Lord asked on

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

* arraysPHP

Avatar of undefined
Last Comment
Alex Lord

8/22/2022 - Mon
Julian Hansen

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

ASKER CERTIFIED SOLUTION
Chris Stanyon

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
Alex Lord

Chris example worked the best, Julian ur one didn't agree with my set-up but a good learning curve for potential projects.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes