skip to Main Content

I want to get all the script that are loaded in my screen

    <script  type="text/javascript"  src="http://url/pub/static/frontend/Magento/luma/en_US/requirejs/require.js"></script>
    <script  type="text/javascript"  src="http://url/pub/static/frontend/Magento/luma/en_US/mage/requirejs/mixins.js"></script>
    <script  type="text/javascript"  src="http://url/pub/static/frontend/Magento/luma/en_US/requirejs-config.js"></script>
    <script  type="text/javascript"  src="http://url/pub/static/frontend/Magento/luma/en_US/mage/polyfill.js"></script>


<script type="text/x-magento-init">
{"*":{}}
<script>
<script>
    var BASE_URL = 'http://urlm/index.php/';
    var require = {
        "baseUrl": "http://url/pub/static/frontend/Magento/luma/en_US"
    };
</script>

    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
    'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-KK5JZ25');</script>

the code i am using is

$conditionalJsPattern = '@(?:<script type="text/javascript"|<script)(.*)</script>@msU';

i want to exclude the googletabmanger code
googletabmanger code contains some words like googletagmanager , gtm

how can i do that???

thanks in advance

2

Answers


  1. Maybe, this expression might be close to what you have in mind:

    (<scriptb[^>]*>)(?!.*googletagmanager.com)*?</script>
    

    and you can flag it however you wish.


    If you wish to explore/simplify/modify the expression, it’s been
    explained on the top right panel of
    regex101.com. If you’d like, you
    can also watch in this
    link
    , how it would match
    against some sample inputs.


    Test

    $re = '/(<scriptb[^>]*>)(?!.*googletagmanager.com)*?</script>/s';
    $str = '<script  type="text/javascript"  src="http://url/pub/static/frontend/Magento/luma/en_US/requirejs/require.js"></script>
    <script  type="text/javascript"  src="http://url/pub/static/frontend/Magento/luma/en_US/mage/requirejs/mixins.js"></script>
    <script  type="text/javascript"  src="http://url/pub/static/frontend/Magento/luma/en_US/requirejs-config.js"></script>
    <script  type="text/javascript"  src="http://url/pub/static/frontend/Magento/luma/en_US/mage/polyfill.js"></script>
    
    <script>(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
    new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
    j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
    'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
    })(window,document,'script','dataLayer','GTM-KK5JZ25');</script>';
    
    preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
    
    var_dump($matches);
    
    Login or Signup to reply.
  2. First of all, use should use appropriate tool for parsing HTML (XML in general), regex is very bad idea for that.

    But if you HAVE TO use regex, I’d suggest such pattern: (?s)<script.+?</script>

    Explanation:

    (?s) – single line mode: . matches newline

    <script – match <script literally

    .+? – match one or more of any character (including newline), non-greedy (as least as possible)

    </script> – match </script> literally

    Demo

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search