Created
February 7, 2014 14:48
-
-
Save 3F/8863945 to your computer and use it in GitHub Desktop.
PHP Generators. e.g.: getSymbol
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
$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