<?php
function convertHashtags($str) {
$regex = '/@([a-zA-Z0-9]+[\sa-zA-Z0-9]*)/';
if(preg_match($regex,$str,$matches) === 1){
list($username,$name) = [$str , strtolower(str_replace(' ','',$matches[1]))];
return "<a href='profile?id=$name'>$username</a>";
}
throw new Exception('Unable to find username in the given string');
}
$string = 'I am @Marie Lee, nice to meet you!';
$string = convertHashtags($string);
echo $string;
- Output for 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
- <a href='profile?id=marielee'>I am @Marie Lee, nice to meet you!</a>
preferences:
124.64 ms | 407 KiB | 5 Q