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

3

我有一个细节数组:

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

但它并没有起作用。

5个回答

2

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
        )

)

1
抱歉,这不是我需要的。这只是删除标签,再看看我需要什么——将<b></b>中的所有内容移动到键中,并从值中删除它。 - Arnas Pečelis
这将删除标签,但他希望标签中的文本用作段落其余部分的关键。 - Orangepill

1

您可以尝试使用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
        )
)

这就是我一直在寻找的。正则表达式做得很好! - Arnas Pečelis

0
假设冒号始终存在,您可以使用strip_tags和explode来获取所需内容。
<?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);

或者在这里运行它


不是每次都会有“:”,所以这不是一个选项:/ - Arnas Pečelis

0

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

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

任何其他解决方案都将被接受,甚至正则表达式也可以!


是的,这也能工作,但代码看起来像我女友的妹妹一样丑。你为什么要问这个? - Arnas Pečelis
因为$this->是指向类的,而我在你发布的代码中没有看到它。 - kamal pal

0

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

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

?>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接