PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/_octopress/source/functions/fgetcsv/index.markdown

https://gitlab.com/orvi2014/phpjs
Markdown | 68 lines | 55 code | 13 blank | 0 comment | 0 complexity | d3004f6ed49fdf99dd44a0d6b75f648d MD5 | raw file
  1. ---
  2. layout: page
  3. title: "JavaScript fgetcsv function"
  4. comments: true
  5. sharing: true
  6. footer: true
  7. alias:
  8. - /functions/view/fgetcsv:801
  9. - /functions/view/fgetcsv
  10. - /functions/view/801
  11. - /functions/fgetcsv:801
  12. - /functions/801
  13. ---
  14. <!-- Generated by Rakefile:build -->
  15. A JavaScript equivalent of PHP's fgetcsv
  16. {% codeblock filesystem/fgetcsv.js lang:js https://raw.github.com/kvz/phpjs/master/functions/filesystem/fgetcsv.js raw on github %}
  17. function fgetcsv (handle, length, delimiter, enclosure, escape) {
  18. // http://kevin.vanzonneveld.net
  19. // + original by: Brett Zamir (http://brett-zamir.me)
  20. // - depends on: str_getcsv
  21. // * example 1: fopen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r');
  22. // * example 1: fgetcsv(handle, 1);
  23. // * returns 1: '<'
  24. var start = 0,
  25. fullline = '';
  26. if (!this.php_js || !this.php_js.resourceData || !this.php_js.resourceDataPointer || length !== undefined && !length) {
  27. return false;
  28. }
  29. start = this.php_js.resourceDataPointer[handle.id];
  30. if (start === undefined || !this.php_js.resourceData[handle.id][start]) {
  31. return false; // Resource was already closed or already reached the end of the file
  32. }
  33. fullline = this.php_js.resourceData[handle.id].slice(start, this.php_js.resourceData[handle.id].indexOf('\n', start) + 1);
  34. if (fullline === '') {
  35. fullline = this.php_js.resourceData[handle.id].slice(start); // Get to rest of the file
  36. }
  37. length = (length === undefined || fullline.length < length) ? fullline.length : Math.floor(length / 2) || 1; // two bytes per character (or surrogate), but ensure at least one
  38. this.php_js.resourceDataPointer[handle.id] += length; // Leaves the pointer one higher apparently than in fgets/fgetss
  39. return this.str_getcsv(this.php_js.resourceData[handle.id].substr(start, length), delimiter, enclosure, escape);
  40. }
  41. {% endcodeblock %}
  42. - [view on github](https://github.com/kvz/phpjs/blob/master/functions/filesystem/fgetcsv.js)
  43. - [edit on github](https://github.com/kvz/phpjs/edit/master/functions/filesystem/fgetcsv.js)
  44. ### Example 1
  45. This code
  46. {% codeblock lang:js example %}
  47. fopen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r');
  48. fgetcsv(handle, 1);
  49. {% endcodeblock %}
  50. Should return
  51. {% codeblock lang:js returns %}
  52. '<'
  53. {% endcodeblock %}
  54. ### Other PHP functions in the filesystem extension
  55. {% render_partial _includes/custom/filesystem.html %}