Skip to content

Instantly share code, notes, and snippets.

@thephucit
Created June 21, 2019 07:55
Show Gist options
  • Select an option

  • Save thephucit/bb48846440e4dae8dca7ea57f695860c to your computer and use it in GitHub Desktop.

Select an option

Save thephucit/bb48846440e4dae8dca7ea57f695860c to your computer and use it in GitHub Desktop.
some helper function
<?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