PHP move bold text to array
I have an array of data:
[info_details] => Array
(
[0] => <b>title:</b> this is title
[1] => <b>name:</b> this is name
[2] => <b>created</b> this is date
)
and I need to format this array:
[info_details] => Array
(
[title] => this is title
[name] => this is name
[created] => this is date
)
so what's the best way to blow up bold text? my code now:
foreach ( $array as $key => $value ) {
$this->__tmp_data['keep'][] = preg_split('/<b[^>]*>/', $value);
}
but that won't work.
source to share
One can try regex with preg_match()
andstr_replace()
$pattern = "/<b>.+:<\/b>\s?/";
$arr['info_details'] = [
'<b>title:</b> this is title',
'<b>name:</b> this is name',
'<b>created:</b> this is date',
];
$new_arr['info_details'] = [];
foreach($arr['info_details'] as $val){
preg_match($pattern, $val, $m);
$new_arr['info_details'][trim(strip_tags($m[0]), ': ')] = str_replace($m[0], '', $val);
}
print '<pre>';
print_r($new_arr);
print '</pre>';
Output
Array
(
[info_details] => Array
(
[title] => this is title
[name] => this is name
[created] => this is date
)
)
source to share
PHP has a built-in function strip_tags()
to strip HTML tags.
foreach ( $array as $key => $value ) {
$this->__tmp_data['keep'][] = strip_tags($value);
}
UPDATE
<?php
$info_details = array
(
'<b>title:</b> this is title',
'<b>name:</b> this is name',
'<b>created:</b> this is date'
);
$tmp_data = [];
foreach ( $info_details as $key => $value ) {
list($key,$value)=explode('</b>', $value);
$tmp_data['keep'][str_replace(array(':','<b>'),'',$key)] = $value;
}
echo '<pre>';
print_r($tmp_data);
?>
OUTPUT
Array
(
[keep] => Array
(
[title] => this is title
[name] => this is name
[created] => this is date
)
)
source to share
Assuming the colon will always be present, you can use strip_tags and blow up to get what you want.
<?php
$info_details = array(
"<b>title:</b> this is title",
"<b>name:</b> this is name",
"<b>created:</b> this is date"
);
$return = array();
foreach($info_details as $val){
list ($key, $value) = explode(":",strip_tags($val), 2);
$return[$key] = $value;
}
print_r($return);
See live here . It's also worth noting that this implementation will remove: from the array key and remove any html content from the trailing part of each array element.
If you cannot rely on the delimiter to be there, you can instead use the close bold shortcut as the delimiter.
<?php
$info_details = array(
"<b>title:</b> this is title",
"<b>name:</b> this is name",
"<b>created</b> this is date"
);
$return = array();
foreach($info_details as $val){
list ($key, $value) = explode("</b>",$val, 2);
$key = strip_tags($key);
$return[$key] = $value;
}
print_r($return);
or run here
source to share
So I found a solution, but this is hard code ...
foreach ( $array as $key => $value ) {
$this->__tmp_data['keep'][strtolower(str_replace(':', '', strip_tags(@reset(explode('</b>', $value)))))] = trim(@end(explode('</b>', $value)));
}
any other decisions will be accepted, even regex is welcome!
source to share
reading the above comments I think this is what you need.
<?php
$info_details = array
(
'<b>title:</b> this is title',
'<b>name:</b> this is name',
'<b>created:</b> this is date'
);
foreach ($info_details as $value)
{
$temp = explode("</b>",$value);
$info_details = array(strip_tags(str_replace(":","",$temp[0])) =>$temp[1]);
}
print_r($info_details);
?>
source to share