Created
January 29, 2015 00:59
-
-
Save jbboehr/0c42160f5a2b35fbdd33 to your computer and use it in GitHub Desktop.
mb_fgets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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