开发者

remove all local javascript file except external javascript files in php

i am writing a php script that compress local javascript files into one from an html page. Now i want to delete local references except external javascript files.

for example, i have following variables.

$my_domain = "mysite";
$base_url ="http://www.mysite.com"

I want to delete all local javascript references except external. local javascript include sub-domain too开发者_如何学运维. For Example, http://www.mysite.com/script/jquery.js and http://dev.mysite.com/scripts/test.js is the example of local javascript files.

i want to use regular expression for this.

EDIT: The format is :

 <script src="http://www.mysite.com/jsfile.js"></script>

EDIT 2: The script in a page is like:

<script type="text/javascript" src="http://localhost/test/example/Scripts/superfish/js/hoverIntent.js"></script>
<script type="text/javascript" src="http://localhost/test/example/Scripts/superfish/js/superfish.js"></script>

where $baseURL="http://localhost/test/example";

it is currently not replacing.


Assuming your base url is mysite.com, this regex:

Search for: (<script\b[^[><]*\ssrc\s*=\s*["'])(?:http:\/\/)?(?:www.)?(?:\w+\.)?\bmysite\.com/?\b([^><"']*["'])

Replace with: $1$2

will make your URL references (can contain any number of domains/subdomains), e.g. from http://www.mysite.com/jsfile.js become jsfile.js

Supposing you want to remove every SCRIPT tags (and their contents) containing such a url, use this regex:

Search for: <script\b[^><]*\ssrc\s*=\s*[^><]*\bmysite\.com\b[^><]*>\s*<\/script>

Replace with: nothing

If there is a possibility that between <script> and </script> can contain any text, use this regex instead:

Search for: <script\b[^><]*\ssrc\s*=\s*[^><]*\bmysite\.com\b[^><]*>.*?<\/script>

Replace with: nothing

So

<?php
$ptn = "/<;script\b[^>;<;]*\ssrc\s*=\s*[^>;<;]*\bmysite\.com\b[^>;<;]*>;.*?<;\/script>;/";
$str = "<;script src="http://www.mysite.com/jsfile.js">;<;/script>;";
$rpltxt = "";
echo preg_replace($ptn, $rpltxt, $str);
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜