Matt Ellwood
Matt Ellwood

Reputation: 103

Convert @mention substrings to clickable hyperlinks

How would I get this to include words that start with capital letters and lowercase? Because right now it is only lowercase.

$string = "you would love this @matt";
$pattern = '/(^|\W)(@([a-z]+))/';
$replacement = '$1<a href="/user/$3">$2</a>';
echo preg_replace($pattern, $replacement, $string);

Upvotes: -1

Views: 155

Answers (1)

John Conde
John Conde

Reputation: 219804

Change [a-z] to [a-zA-Z]:

$pattern = '/(^|\W)(@([a-zA-Z]+))/';

Or use the i modifier (it means make the match case insenstivive):

$pattern = '/(^|\W)(@([a-z]+))/i';

Upvotes: 2

Related Questions