drsqpko5286 2019-05-22 21:41 采纳率: 0%
浏览 43

用于将标签转换为BBCode的RegEx [重复]

This question already has an answer here:

I have a form which takes HTML, converts it to BBCode and stores it in the database.

Most tag renaming is easily handled with a simple switch, however, text alignment is causing me some trouble.

HTML is <div style="text-align: right;"></div> and I need to convert to BBcode [right][/right]

So I can easily do str_replace on the opening tag but for the closing tag I am replacing but that could be [/left],[/center] or [/right]. I need to know what the opening tag is before I can set it, hence the problem.

I am bad at simple regex so this one is even more difficult.

In logic terms I am trying to do this:

$str = str_replace("</div>","$align_value",$str);

But I need to know what the opening tag is to set the correct closing tag.

The expected result is it will check what the opening tag is:

if($opening_tag = '<div style="text-align: right;">')
{
 $closing_tag = '[/right]';
} else if($opening_tag = '<div style="text-align: center;">')
{
 $closing_tag = '[/center]';
} else if($opening_tag = '<div style="text-align: left;">')
{
 $closing_tag = '[/left]';
} else {
  // Some other div that isn't aligned so do nothing
}

But they key is being able to find what the opening tag is first. Any help appreciated

</div>
  • 写回答

1条回答 默认 最新

  • douqianni4080 2019-05-22 22:28
    关注

    It may not be the best idea to solve this problem with regular expressions. However, if you wish to do so, we want to get the attribute value, which I'm guessing that it would be always left, right and center, then collect our element textContents, store it in two capturing groups and then add our desired tags to it, maybe similar to:

    <.+?:\s+([a-z]+);">(.+?)<\/div>
    

    We can also change the div closing tag with a more broad expression, if necessary:

    <.+?:\s+([a-z]+);">(.+?)<\/.+?>
    

    enter image description here

    Demo

    const regex = /<.+?:\s+([a-z]+);">(.+?)<\/div>/gm;
    const str = `<div style="text-align: right;">Anything you wish here</div>
    <div style="text-align: center;">Anything you wish here</div>
    <div style="text-align: left;">Anything you wish here</div>
    <div style="text-align: center;">Anything you wish here</div><div style="text-align: right;">Anything you wish here</div>`;
    const subst = `[$1]$2[/$1]`;
    
    // The substituted value will be contained in the result variable
    const result = str.replace(regex, subst);
    
    console.log('Substitution result: ', result);

    DEMO

    PHP

    $re = '/<.+?:\s+([a-z]+);">(.+?)<\/.+?>/m';
    $str = '<div style="text-align: right;">Anything you wish here</div>
    <div style="text-align: center;">Anything you wish here</div>
    <div style="text-align: left;">Anything you wish here</div>
    <div style="text-align: center;">Anything you wish here</div><div style="text-align: right;">Anything you wish here</div>';
    $subst = '[$1]$2[/$1]';
    
    $result = preg_replace($re, $subst, $str);
    
    echo $result;
    

    RegEx

    If this expression wasn't desired, it can be modified or changed in regex101.com.

    RegEx Circuit

    jex.im visualizes regular expressions:

    enter image description here


    Based on Niet the Dark Absol's advice in the comment, this method would not work with nested tags.

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据