编程语言
首页 > 编程语言> > PHP将粗体文本移到数组键

PHP将粗体文本移到数组键

作者:互联网

我有详细信息数组:

[info_details] => Array
            (
                [0] => <b>title:</b> this is title
                [1] => <b>name:</b> this is name
                [2] => <b>created</b> this is date
            )

我需要将此数组格式化为:

[info_details] => Array
            (
                [title] => this is title
                [name] => this is name
                [created] => this is date
            )

那么爆炸粗体的最佳方法是什么?
我的代码现在:

foreach ( $array as $key => $value ) {
      $this->__tmp_data['keep'][] = preg_split('/<b[^>]*>/', $value);
}

但这不起作用.

解决方法:

可以使用preg_match()和str_replace()尝试使用regex

$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>';

输出量

Array
(
    [info_details] => Array
        (
            [title] => this is title
            [name] => this is name
            [created] => this is date
        )
)

标签:explode,preg-split,arrays,php
来源: https://codeday.me/bug/20191120/2040279.html