Skip to content

Instantly share code, notes, and snippets.

@jbboehr
Created January 29, 2015 00:59
Show Gist options
  • Save jbboehr/0c42160f5a2b35fbdd33 to your computer and use it in GitHub Desktop.
Save jbboehr/0c42160f5a2b35fbdd33 to your computer and use it in GitHub Desktop.
mb_fgets
<?php
function mb_fgets($fh)
{
$buf = '';
$pos = false;
$fstart = ftell($fh);
while( !feof($fh) ) {
$str = fread($fh, 256);
if( $str === false ) {
break;
}
$buf .= $str;
$pos = mb_strpos($buf, "\n");
if( $pos !== false ) {
break;
}
}
// Not found, return the rest of string
if( $pos === false ) {
return $buf ? $buf : false;
}
// Return up to and including the newline, and rewind the file descriptor
$ret = mb_substr($buf, 0, $pos);
fseek($fh, $fstart + $pos + 1, SEEK_SET);
return $ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment