mysql - PHP explode to detect username -


i'm trying build php code detect twitter-like mention, rather linking @username, i'm locating user's id , referring that.

function get_user_id_from_username($username) {     $username = sanitise($username);     return mysql_result(mysql_query("select user_id users user_sitename = '$username'"), 0, 'user_id'); }  $string = sanitise($_post['updatetext']); $explosion = explode("@", $string); $count = count($explosion);  for($i = 0; $i < $count; $i++){     $explosion2 = explode(" ", $explosion[$i]);     $explosion2 = $explosion2[0];      $username_id = get_user_id_from_username($explosion2);      $string = str_replace("@{$explosion2}", "<a href='profile.php?id={$username_id}'>{$explosion2}</a>", $string); } 

the issue i'm having username detected if @username isn't followed punctuation, if $update equal "hello @username, ok?" query won't find username equal "username," , throws error.

is there way can end explode once piece of punctuation exists? rather $explosion2 being equal "username," it's equal "username"?

instead of using explode, do:

preg_match_all('/@[a-za-z]+/',$string,$usernames); 

that give array of usernames, $usernames can loop on db lookup. note usernames include @ - you'll need strip out before doing lookup.

i've assumed usernames consist entirely of letters - if want allow numbers or other characters adjust regexp accordingly.


Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -