Use PHP functions in JavaScript

JavaScript stream_get_line

Read up to maxlen bytes from a stream or until the ending string is found

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
function stream_get_line (handle, length, ending) {
    // Read up to maxlen bytes from a stream or until the ending string is found  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/stream_get_line    // +   original by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: fopen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r');
    // *     example 1: stream_get_line(handle, 2);
    // *     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
    } 
    // Fix: Should we also test for /\r\n?|\n/?
    fullline = this.php_js.resourceData[handle.id].slice(start, this.php_js.resourceData[handle.id].indexOf(ending, 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;    return this.php_js.resourceData[handle.id].substr(start, length-(fullline && ending && ending.length ? ending.length : 0));
}
external links: original PHP docs | raw js source

Examples

Running

1
2
fopen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r');
stream_get_line(handle, 2);

Should return

1
'<'

Dependencies

No dependencies, you can use this function standalone.

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 stream_get_line goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

No comments yet. Be the first!


Contribute a New function

More functions

In this category

stream_context_create
stream_context_get_default
stream_context_get_options
stream_context_get_params
stream_context_set_default
stream_context_set_option
stream_context_set_params
stream_get_contents
» stream_get_line
stream_is_local

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: @HalfWinter, Paulo Freitas, Andros Peña Romo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !