phpjs
Version:
56 lines (49 loc) • 2.17 kB
Markdown
---
layout: page
title: "JavaScript php_strip_whitespace function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/php_strip_whitespace:487
- /functions/view/php_strip_whitespace
- /functions/view/487
- /functions/php_strip_whitespace:487
- /functions/487
---
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's php_strip_whitespace
{% codeblock misc/php_strip_whitespace.js lang:js https://raw.github.com/kvz/phpjs/master/functions/misc/php_strip_whitespace.js raw on github %}
function php_strip_whitespace (file) {
// http://kevin.vanzonneveld.net
// + original by: Brett Zamir (http://brett-zamir.me)
// % note 1: This function uses XmlHttpRequest and cannot retrieve resource from different domain.
// % note 1: Synchronous so may lock up browser, mainly here for study purposes.
// % note 1: To avoid browser blocking issues's consider using jQuery's: $('#divId').load('http://url') instead.
// - depends on: file_get_contents
// * example 1: php_strip_whitespace('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm');
// * returns 1: '123'
try {
var str = this.file_get_contents(file);
} catch (e) {
return '';
}
// Strip comments (both styles), reduce non-newline whitespace to one, reduce multiple
// newlines (preceded by any whitespace) to a newline, remove WS at beginning of line,
// and at end of line
return str.replace(/\/\/.*?\n/g, '').replace(/\/\*[\s\S]*?\*\//g, '').replace(/[ \f\r\t\v\u00A0\u2028\u2029]+/g, ' ').replace(/\s*\n+/g, '\n').replace(/^\s+/gm, '').replace(/\s*$/gm, '');
}
{% endcodeblock %}
- [view on github](https://github.com/kvz/phpjs/blob/master/functions/misc/php_strip_whitespace.js)
- [edit on github](https://github.com/kvz/phpjs/edit/master/functions/misc/php_strip_whitespace.js)
### Example 1
This code
{% codeblock lang:js example %}
php_strip_whitespace('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm');
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
'123'
{% endcodeblock %}
### Other PHP functions in the misc extension
{% render_partial _includes/custom/misc.html %}