【问题标题】:PHP move bold text to array keyPHP将粗体文本移动到数组键
【发布时间】:2015-10-15 11:05:09
【问题描述】:

我有详细信息数组:

[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 arrays explode preg-split


    【解决方案1】:

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

    【讨论】:

    • 这不是我需要的,抱歉。这只会删除标签,然后再看看我需要什么 - 将 中的所有内容移动到键并将其从值中删除
    • 这将删除标签,但他希望标签中的文本用作段落中其余文本的键。
    【解决方案2】:

    假设冒号始终存在,您可以使用 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);
    

    现场观看here。另外值得注意的是,此实现将从数组键中删除 : 并从每个数组元素的尾随部分去除任何 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);
    

    或运行它here

    【讨论】:

    • 不,不是每次都有“:”。所以这不是一个选择:/
    【解决方案3】:

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

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

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

    【讨论】:

    • 是的,这也行得通,但是代码看起来像我的 gf 姐姐一样丑陋。你为什么这么问?
    • 因为 $this-&gt; 指的是类 .. 而我在您发布的代码中没有看到。
    【解决方案4】:

    可以试试这个regexpreg_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
            )
    )
    

    【讨论】:

    • 这就是我要找的。正则表达式做得很好!
    【解决方案5】:

    阅读上面的 cmets 我想这就是你所需要的。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-04
      • 2020-10-17
      • 2014-07-13
      • 1970-01-01
      相关资源
      最近更新 更多