Created
June 21, 2019 07:55
-
-
Save thephucit/bb48846440e4dae8dca7ea57f695860c to your computer and use it in GitHub Desktop.
some helper function
This file contains hidden or 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 | |
| /** | |
| * detect hastag from string | |
| * | |
| * @param [string] $string | |
| * @return [array] | |
| */ | |
| public function detectHastag($string) | |
| { | |
| $pattern = '/\s*#+\s*/im'; | |
| if (preg_match_all($pattern, $string, $matches)) { | |
| return $matches[0]; | |
| } | |
| return null; | |
| } | |
| /** | |
| * convert hashtag to number | |
| * | |
| * @param [string] $hash | |
| * @param [number] $default | |
| * @return [string] | |
| */ | |
| public function hastagToNumber($hash, $default = null) | |
| { | |
| $number = ''; | |
| $zero = strlen($hash); | |
| if (! $default) { | |
| $number = sprintf("%0".$zero."s", 1); | |
| } else { | |
| if (strlen($hash) <= strlen($default)) { | |
| $number = $default; | |
| } else { | |
| $number = sprintf("%0".$zero."s", $default); | |
| } | |
| } | |
| return $number; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment