dpi10335 2013-11-24 00:51
浏览 43

删除字符串特定部分之间的空格/换行符

I'm trying to make it so a user can add a table into their posts, the problem i'm running into, in another function i'm using nl2br to make it so it automatically add line breaks. Though, how tables work, all the line breaks will go above it causing massive amounts of space. I supplied an image below for you to be able to see what I mean. I tried using or , but I feel like i'm either using it wrong or it doesn't work in this situation.

function markupDoc($post){
    //tables
    while(($post2 = preg_replace("/\[td]((?:(?!\[td\]).)+?)\[\/td\]/s", '<td>\\1</td>', $post)) !== $post){
        $post=$post2;
    }
    while(($post2 = preg_replace("/\[tr]((?:(?!\[tr\]).)+?)\[\/tr\]/s", "<tr>\\1</tr>", $post)) !== $post){
        $post=$post2;
    }
    while(($post2 = preg_replace("/\[thead]((?:(?!\[thead\]).)+?)\[\/thead\]/s", '<thead>\\1</thead>', $post)) !== $post){
        $post=$post2;
    }
    while(($post2 = preg_replace("/\[table]((?:(?!\[table\]).)+?)\[\/table\]/s", '<table class="table table-striped table-bordered">\\1</table>', $post)) !== $post){
        $post=$post2;
    }   
    return $post;
}

String i'm submitting:

going to
[table]
    [thead]
        [td]test[/td]
        [td]test[/td]
        [td]moo[/td]
        [td]a[/td]
    [/thead]
    [tr]
        [td]test[/td]
        [td]moo[/td]
    [/tr]
    [tr]
        [td]test[/td]
        [td]test[/td]
        [td]moo[/td]
        [td]a[/td]
    [/tr]
[/table]

See the extra space

  • 写回答

1条回答 默认 最新

  • dongxian3574 2013-11-24 09:01
    关注

    Add another function which will take care of different parts of your post. Since the part between [table] and [\table] clearly differs from the rest you should do something like:

    function parsePost($post)
    {
        $table_start = mb_strpos($post, '[table]');
        $table_end = mb_strpos($post, '[/table]', $table_start);
        $table_end += mb_strlen('[/table]');
    
        $output = nl2br(mb_substr($post, 0, $table_start));
        $output .= markupDoc(mb_substr($post, $table_start, $table_end - $table_start));
        $output .= nl2br(mb_substr($post, $table_end));
        return $output;
    }
    

    Test code: http://ideone.com/sfRn33

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)