Skip to content

Instantly share code, notes, and snippets.

@andreconghau
Created August 22, 2019 06:31
Show Gist options
  • Save andreconghau/556323cb51cb1ba98d1b3f5468a33f6a to your computer and use it in GitHub Desktop.
Save andreconghau/556323cb51cb1ba98d1b3f5468a33f6a to your computer and use it in GitHub Desktop.
get HashTag
<?php
function convertAll($str) {
$regex = "/[@#](\w+)/";
$hrefs = [
'#' =&gt; 'hashtag?tag',
'@' =&gt; 'profile?username'
];
$result = preg_replace_callback($regex, function($matches) use ($hrefs) {
return sprintf(
'&lt;a href="%s=%s"&gt;%s&lt;/a&gt;',
$hrefs[$matches[0][0]],
$matches[1],
$matches[0]
);
}, $str);
return($result);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment