Skip to content

Instantly share code, notes, and snippets.

@3F
Created February 7, 2014 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 3F/8863945 to your computer and use it in GitHub Desktop.
Save 3F/8863945 to your computer and use it in GitHub Desktop.
PHP Generators. e.g.: getSymbol
$str = "learning trails provide you with everything you need to..";
foreach(Util::getSymbol($str, 0) as $cur){
echo Util::charToHex($cur); // 6C6561726E696E672074...
}
...
/**
* @param string $str
* @param int $start
*/
public static function getSymbol(&$str, $start = 0)
{
while(isset($str{$start})){
yield $str{$start++};
}
}
public static function charToHex($char)
{
return sprintf("%02X", ord($char));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment