I'm working on building a social network from HTML, PHP, and a MySQL database, and I cant figure how to make this PHP code to work. Please remember, I'm totally not a pro at PHP.
What I'm working on now is a function, atreplace($text)
, that has the preg_replace()
function in it to find the @ mentions in a post caption, comment, or wherever else I use it, to make them into clickable links. Now that's pretty easy, except the @mentions in the strings it would process are user id numbers instead of the user name. For example, a string in a post caption for my social network would look like this:
"Went to Disneyland with my friends @214432 and @163728 today.". But what the post caption originally said was this: "Went to Disneyland with my friends @billysmith and @hi_im_kelly today.".
I wrote the script (not included because it doesn't have to do with the question) that processes the post data and inserts it into the MySQL database to replace the @ mentions to be the user number id of the user that was mentioned instead of the user name in situations where people would have changed their username, then it wouldn't have to edit peoples posts where they mentioned someone that changed their username.
Problem
When it uses the atreplace()
function to echo out the post caption when someone is viewing their feed, of course it will echo out "@214432" and "@163728" (from the example above) in the post caption instead of "@billysmith" and "@hi_im_kelly", which I would expect, because a different php script edited it and changed the usernames that were mentioned to be the user id, which is a number.
This is what I want
I want to write a function like the one I have below (get_username($id)
). I only included get_username($id)
to show what I want to do. I dont really know a lot about how to use the preg_replace()
function yet.
As you can see, I tried passing the $1, which would be the user number id that I want it to replace with the username, with the get_username('$1)
function, but it doesn't work. (I know I used that $1 wrong somehow). Although in the get_username()
function, when I try returning the $id, it does output the user number id right. But when I used the $id in the mysql_query(), it didn't even show up.
In case you didn't understand all that, first, I want to put the string that has the @ mentions in it, into the atreplace()
function. This function will find all the @ user mentions, remember, they are the users number id. Then, it will put each mention into the get_username()
function. That function will get the username from a mysql database where the id equals the user number id that the atreplace()
function found, then it will return the username, and in the end, will replace the @ mention...
Can someone please show me how I could change the get_username()
function to make it work? Or just write a new one.
<?php
function get_username($id){
$undata = mysql_query("select `username` from `accounts` where `id`='$id';");
$un = mysql_fetch_assoc($undata);
return "<a href='#'>@".$un['username']."</a>";
}
function atreplace($text){
$replaced = preg_replace('/\@(\w+)/', get_username('$1'), $text);
return($replaced);
}
?>