dpxo13079 2015-07-24 09:13
浏览 73
已采纳

PHP将粗体文本移动到数组键

I have details array:

[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 to this:

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

so what is the best way to explode bold text? my code now:

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

but it doesn't work.

  • 写回答

5条回答 默认 最新

  • dongyan2469 2015-07-24 09:42
    关注

    Can try this regex with preg_match() and str_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
            )
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部