如何创建正则表达式模式以删除特定标签后的换行符

时间:2019-06-07 10:15:47

标签: php regex

如何在{literal}标签的每个结尾处删除一个换行符?

我尝试使用此正则表达式模式(?<=\{literal\}|\{\/literal\}\s)\s+,但问题是它没有保留空格或制表符。例如下面的

<script>
    {literal}

    (function () {
        //カレンダー
        var now = 0;

        $(window).on('load', function () {
            abc();
            $('.holiday').each(function () {
                if ($(this).val === '') {
                    $(this).removeClass('holiday');
                }
            });
        });

        $('#next').on('click', function () {
            now++;
            abc(now);
        });

        $('#prev').on('click', function () {
            now--;
            abc(now);
        });
    })();


{/literal}
</script>
<script src="/scripts/pc_functions.js"></script>

<!-- YTM -->
<script type="text/javascript">{literal}
        (function () {
            var tagjs = document.createElement("script");
            var s = document.getElementsByTagName("script")[0];
            tagjs.async = true;
            tagjs.src = "//s.yjtag.jp/tag.js#site=mOjVJxB,c30x29r,64JeBvp,WvJI3VS,mqotrsJ,KJG7ewD,vcS2YQh,DOhPuhc";
            s.parentNode.insertBefore(tagjs, s);
        }()); {/literal}
</script>
<noscript>
    {literal}<iframe src="//b.yjtag.jp/iframe?c=mOjVJxB,c30x29r,64JeBvp,WvJI3VS,mqotrsJ,KJG7ewD,vcS2YQh" width="1"
        height="1" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>{/literal}
</noscript>

这是我想要实现的

<script>
    {literal}
    (function () {
        //カレンダー
        var now = 0;

        $(window).on('load', function () {
            abc();
            $('.holiday').each(function () {
                if ($(this).val === '') {
                    $(this).removeClass('holiday');
                }
            });
        });

        $('#next').on('click', function () {
            now++;
            abc(now);
        });

        $('#prev').on('click', function () {
            now--;
            abc(now);
        });
    })();


{/literal}</script>
<script src="/scripts/pc_functions.js"></script>

<!-- YTM -->
<script type="text/javascript">{literal}        (function () {
            var tagjs = document.createElement("script");
            var s = document.getElementsByTagName("script")[0];
            tagjs.async = true;
            tagjs.src = "//s.yjtag.jp/tag.js#site=mOjVJxB,c30x29r,64JeBvp,WvJI3VS,mqotrsJ,KJG7ewD,vcS2YQh,DOhPuhc";
            s.parentNode.insertBefore(tagjs, s);
        }()); {/literal}
</script>
<noscript>
    {literal}<iframe src="//b.yjtag.jp/iframe?c=mOjVJxB,c30x29r,64JeBvp,WvJI3VS,mqotrsJ,KJG7ewD,vcS2YQh" width="1"
        height="1" frameborder="0" scrolling="no" marginheight="0" marginwidth="0"></iframe>{/literal}</noscript>

1 个答案:

答案 0 :(得分:1)

如果我理解得很好:

$str = preg_replace('~{/?literal}\K\h*\R~', '', $str);

demo