微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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
        )
)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐