0

I want to combine javascript-files into a single one to reduce the amount of requests to the server. Because there is no plugin for that, I want to code something by myself. Unfortunately the CMS has no central functions to include javascript-files which can be overwritten or something like that. So I'm forced to search and replace the script-tags in the source, before php will print the outout to the user.

To get the links I wrote a regex-pattern which matches on any script-tag with src-attribute:

$scriptTagPattern = '#<script (?:.+)?src="([^"]+)">#'; 

I think this should be correct because there are different script-tags. And I'm getting the source from template before they get parsed, so the src-attribute can also include php-code like this:

<script type="text/javascript" src="' . $options['baseurl'] . '/scripts/somejsfile.js"></script>

My pattern is working, but only partly. For example the following scripts are matched:

http://code.jquery.com/jquery-2.1.0.min.js
/scripts/yui/yuiloader-dom-event/yuiloader-dom-event.js

But the following is not matched:

<script type="text/javascript" src="scripts/read_marker.js?v=' . $options['simpleversion'] . '"></script>

I can't understand why my pattern does not match here. For example, the yuiloader (see examples above) has the following source which is similar to the read_marker script:

<script type="text/javascript" src="' . Template::fetchStylevar("yuipath") . '/yuiloader-dom-event/yuiloader-dom-event.js"></script>

2 Answers 2

1

Using regular expressions to extract information from HTML or XML documents is considered bad practice as the regular expression in real life documents are fragile and hard to maintain. Use a DOM parser, in PHP DOMDocument, for this:

$doc = new DOMDocument();
$doc->loadHTML($YOUR_HTML);

foreach($doc->getElementsByTagName('script') as $script) {
    if($script->hasAttribute('src')) {
        echo $script->getAttribute('src') . PHP_EOL;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

This was my first idea too. But this isn't working, the parser beaks because of the php-code inside the src-tag of most elements. So I thought using regular expressions would be better instead of replacing every single variable to its string value.
you need to use a method like <?php insert_script('...') ?> in your templates. Accumulate the scripts in this method and use consequently in your templates.
Yes this would be the best solution. But unfortunately the CMS is using a custom template language, where direct php-code is not allowed. And the other big problem is that the templates must be changed. The plugin should be published later and so it should be as easy as possible to use.
scanning all the templates on every request might be a performance issue
Yes of course. But I want to keep the templates in cache after scanning them. This will also save performance on the database querys.
1

I solved the problem: The regex is fine, I used preg_match() instead of preg_match_all(), so I only got the first file in each template. Simple but effective...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.