Joppx
Joppx

Reputation: 82

How to create a regex pattern to remove line breaks after specific tag

How can I remove one line breaks in every ending of {literal} tag?

I tried using this regex pattern (?<=\{literal\}|\{\/literal\}\s)\s+ but the problem of this is it didn't preserve the spaces or tab. For example below

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

This is what I want to achieve

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

Upvotes: 1

Views: 64

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

If I understand well:

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

demo

Upvotes: 1

Related Questions