Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F425682
jsShrink.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
jsShrink.php
View Options
<?php
/** Remove spaces and comments from JavaScript code
* @param string code with commands terminated by semicolon
* @return string shrinked code
* @link http://vrana.github.com/JsShrink/
* @author Jakub Vrana, http://www.vrana.cz/
* @copyright 2007 Jakub Vrana
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2 (one or other)
*/
function
jsShrink
(
$input
)
{
return
preg_replace_callback
(
'(
(?:
(`(?:
\\\\
.|[^`
\\\\
])*`) # template literal
|(^|[-+
\(
[{}=,:;!%^&*|?~]|/(?![/*])|return|throw) # context before regexp
(?:
\s
|//[^
\n
]*+
\n
|/
\*
(?:[^*]|
\*
(?!/))*+
\*
/)* # optional space
(/(?![/*])(?:
\\\\
[^
\n
]
|[^[
\n
/
\\\\
]++
|
\[
(?:
\\\\
[^
\n
]|[^]])++
)+/) # regexp
|(^
|
\'
(?:
\\\\
.|[^
\n\'\\\\
])*
\'
|"(?:
\\\\
.|[^
\n
"
\\\\
])*"
|([0-9A-Za-z_$]+)
|([-+]+)
|.
)
)(?:
\s
|//[^
\n
]*+
\n
|/
\*
(?:[^*]|
\*
(?!/))*+
\*
/)* # optional space
)sx'
,
'jsShrinkCallback'
,
"$input
\n
"
);
}
function
jsShrinkCallback
(
$match
)
{
static
$last
=
''
;
$match
+=
array_fill
(
1
,
7
,
null
);
// avoid E_NOTICE
list
(,
$template
,
$context
,
$regexp
,
$result
,
$word
,
$operator
)
=
$match
;
if
(
$template
)
{
$result
=
$template
;
}
elseif
(
$word
!=
''
)
{
$result
=
(
$last
==
'word'
?
"
\n
"
:
(
$last
==
'return'
?
" "
:
""
))
.
$result
;
$last
=
(
$word
==
'return'
||
$word
==
'throw'
||
$word
==
'break'
||
$word
==
'async'
?
'return'
:
'word'
);
}
elseif
(
$operator
)
{
$result
=
(
$last
==
$operator
[
0
]
?
"
\n
"
:
""
)
.
$result
;
$last
=
$operator
[
0
];
}
else
{
if
(
$regexp
)
{
$result
=
$context
.
(
$context
==
'/'
?
"
\n
"
:
""
)
.
$regexp
;
}
$last
=
''
;
}
return
$result
;
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 3, 12:46 AM (1 d, 20 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55350
Default Alt Text
jsShrink.php (1 KB)
Attached To
Mode
rP Phorge
Attached
Detach File
Event Timeline
Log In to Comment