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
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

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
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Alex Lord
Alex Lord

ASKER

Chris example worked the best, Julian ur one didn't agree with my set-up but a good learning curve for potential projects.
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo