Skip to content

Instantly share code, notes, and snippets.

@clue
Forked from WyriHaximus/gist:11254879
Last active August 29, 2015 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save clue/11254932 to your computer and use it in GitHub Desktop.
Save clue/11254932 to your computer and use it in GitHub Desktop.
$buffer = '';
$conn->on('data', function($data, $conn) use (&$buffer) {
$buffer .= $data;
// check to see if the buffer contains any line feeds (detect end of line)
while (false !== ($pos = strpos($buffer, PHP_EOL))) {
// get the first line from the buffer (up to the EOL)
$line = substr($buffer, 0, $pos);
// remove the line from the buffer (advance behind the EOL)
$buffer = (string)substr($buffer, $pos + strlen(EOL));
// do something with $line
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment