Username Mentioning in PHP
Added by astuffedtiger on May 16, 2017 19:24
<?php
/* From here: http://stackoverflow.com/questions/7671996/how-to-parse-user-mentions-in-php */
$text = 'say hello to @elise_123.com and @john-abc';
function replace_at_symbol($matches){
	$username = str_replace("@", "", $matches[0]);
	return '<a href="/profile/'.$username.'">'.$matches[0].'</a>';
}

$output = preg_replace_callback("/([@][-_a-zA-Z-0-9]+)/", "replace_at_symbol", $text);
echo $output;
?>

Comments