I got an array from a database with products and I want to split/seperate the large description text below in smaller chunks of product attribute name and value. Ultimately I'm striving for database normalization, as I'm currently trying to create a import tool for 2 different database designs.
The array I get from old product table:
Array
(
[0] => Array
(
[product_id] => 219
[product_description] =>
<table style="color:; text-align: left;">
<tr>
<td>
Processor:
</td>
<td>
Intel Core 2 Duo - E8400
</td>
</tr>
<tr>
<td>
Clock speed:
</td>
<td>
3.0 GHz
</td>
</tr>
<tr>
<td>
Memory:
</td>
<td>
4 GB
</td>
</tr>
<tr>
<td>
Hard disk:
</td>
<td>
250 GB
</td>
</tr>
<tr>
<td>
Video-adapter:
</td>
<td>
VGA, Display
</td>
</tr>
<tr>
<td>
Netwerk card:
</td>
<td>
1000 Mbps LAN
</td>
</tr>
<tr>
<td>
Optical drive:
</td>
<td>
DVD-Rewriter
</td>
</tr>
<tr>
<td>
Operating system:
</td>
<td>
Windows 7 or 10 Pro
</td>
</tr>
<tr>
<td>
Warranty:
</td>
<td>
1 year
</td>
</tr>
</table>
)
)
My code so far:
$sth = $dbh->prepare("SELECT * from products WHERE product_status_id = '1' ORDER BY order_num ASC");
$sth->execute();
$result = $sth->fetchAll(PDO::FETCH_ASSOC);
$output = array();
$tdpattern = "!<td>(.*?)</td>!is";
foreach ($result as $key=>$val) {
preg_match_all($tdpattern, $val['product_description'], $result);
foreach ($result as $key => $arr) {
foreach ($arr as $key2 => $description) {
$output[] = preg_replace('/\n^[\x0a\x20]+|[\x0a\x20]+$/','',$description);
}
}
}
// return $output to controller
As you can see below, the ouput shows multiple spaces infront of words but not between them, there are also newlines that should be erased. How can I erase all those control characters such as line feeds and whitespaces except 1 space between words for every array element so ideally it becomes like the layout at the bottom?
Array
(
[0] => Processor
[1] => IntelCore2-E5500
[2] => Clockspeed
[3] => 2.93GHz
[4] => Memory
[5] => 4GB
[6] => Harddisk
[7] => 250GB
[8] => Video-adapter
[9] => VGA,Display
[10] => Netwerkcard
[11] => 1000mbpsLAN
[12] => Opticaldrive
[13] => DVD-Rewriter
[14] => Operatingsystem
[15] => Windows7or10Pro
[16] => Warranty
[17] => 2jaar
)
I wish to convert it to this layout:
[219] => array (
[product_description] => array (
[processor] => Intel Core 2 - E5500
[clock speed] => 2.93 GHz
[memory] => 2.93 GHz
[hard disk] => 2.93 GHz
[video adapter] => 2.93 GHz
[network card] => DVD Rewriter
[optical drive] => DVD Rewriter
[operating system] => Windows 7 or 10 Pro
[warranty] = > 2 years
)
)
Some directions would be great, specifically how to improve the regex.