PHP移动粗体文本到数组键


PHP move bold text to array key

我有细节数组:

[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);
}

PHP内置了strip_tags()函数来剥离HTML标签。

   foreach ( $array as $key => $value ) {
            $this->__tmp_data['keep'][] = strip_tags($value);
   }

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

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

可以用preg_match()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>';

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

假设冒号将始终存在,您可以使用strip_tags和爆炸来获得您想要的。

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

看现场直播。同样值得注意的是,此实现将从数组键中删除:,并从每个数组元素的末尾部分删除任何html内容。

如果不能使用分隔符,则可以使用关闭粗体标记作为分隔符。

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

或者在这里运行

所以我找到了一个解决方案,但这是硬代码…

foreach ( $array as $key => $value ) {
                $this->__tmp_data['keep'][strtolower(str_replace(':', '', strip_tags(@reset(explode('</b>', $value)))))] = trim(@end(explode('</b>', $value)));
            }

任何其他解决方案将被接受,甚至正则表达式是受欢迎的!

看了上面的评论,我猜这就是你需要的。

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