JavaScript fgetss
Get a line from file pointer and strip HTML tags
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 | function fgetss (handle, length, allowable_tags) { // Get a line from file pointer and strip HTML tags // // version: 1109.2015 // discuss at: http://phpjs.org/functions/fgetss // + original by: Brett Zamir (http://brett-zamir.me) // - depends on: strip_tags // * example 1: fopen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r'); // * example 1: fgetss(handle, 4096, 'a'); // * returns 1: '' var start = 0, fullline = ''; if (!this.php_js || !this.php_js.resourceData || !this.php_js.resourceDataPointer || length !== undefined && !length) { return false; } start = this.php_js.resourceDataPointer[handle.id]; if (start === undefined || !this.php_js.resourceData[handle.id][start]) { return false; // Resource was already closed or already reached the end of the file } fullline = this.php_js.resourceData[handle.id].slice(start, this.php_js.resourceData[handle.id].indexOf('\n', start) + 1); if (fullline === '') { fullline = this.php_js.resourceData[handle.id].slice(start); // Get to rest of the file } length = (length === undefined || fullline.length < length) ? fullline.length : Math.floor(length / 2) || 1; // two bytes per character (or surrogate), but ensure at least one this.php_js.resourceDataPointer[handle.id] += length - 1; return this.strip_tags(this.php_js.resourceData[handle.id].substr(start, length), allowable_tags); } |
Examples
Running
1 2 | fopen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r'); fgetss(handle, 4096, 'a'); |
Should return
1 | '' |
Dependencies
In order to use this function, you also need:
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have fgetss goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>