DJCMBear Posted November 30, 2009 Share Posted November 30, 2009 Hi im working on a forum and I have alomost finished it but i want a user quote system like twitter and facebook with the @ sign to start it off, however even though im good at php coding and have done a code with preg_match and preg_replace to change them quotes to link. For example if someone wrote "to quote @123456 which he said this." what would come up is "to quote 123456 which he said this." and the 123456 would be a link to their profile. I also did the username change and got this to work but when I did that the code only got the first quoted id number and if there was two users quoted in one comment then it would have the same username going to two different profile links. What I want to do is what facebook does, I want that @123456 link to change to the user's username. Lets say the id "123456" goes to the user "Test User" then the link would be: <a href="http://tester.com/123456">Test User</a> What I need help with is how to make a function that gets the comment looks through it for each quoted user and looks in the database for that id number and then prints out a link to their profile with the link having their username showing on the page and a link like "http://tester.com/<userid>". For example if the php function was called "comment" then i would do this: print comment('to quote @123456 and @654321 which they said this.'); And what prints out on the page is: to quote Test User and Guest User which they said this. And the html code would be: to quote <a href="http://tester.com/123456">Test User</a> and <a href="http://tester.com/654321">Guest User</a> which they said this. If anyone can help me please reply Thank You. Link to comment https://forums.phpfreaks.com/topic/183356-help-with-forum-quoting/ Share on other sites More sharing options...
cags Posted November 30, 2009 Share Posted November 30, 2009 First things first, assuming I'm understanding you correctly, I'm not sure I personally like the approach. You seem to want the user to type in an id number and then have the system convert the id number into the users name. What are the chances a person will remember the id number of the person they wish to write about? I've never actually tried to create a link using Facebook, but I'd imagine it works exactly opposite to that. Either way, now I've mentioned that, I'll move on to helping you achieve it if you still feel it's the right approach. As far as I can see, your basically going to need to break it down into 3 simple® stages. Firstly you need to find all of the 'links' in the text in quesion. This would probably be best done using preg_match_all. A quick example off the top of my head would use a pattern similar to... #\b@[0-9]+\b# This would return an array of ids. You then need to use all these id's to find the approrpiate usernames. There are dozens of ways of achieving this, I don't honestly know which would be best. I'd probably opt for constructing a query along the lines of... SELECT id, username FROM users WHERE id IN ({$out[0]}, {$out[1]} ... {$out['x']}) Now all you need to do is to replace the values in the string with the new format, best option for that is probably going to be preg_replace. Since all the preg_ functions should match in the same order, you should be able to use the original match pattern, with a replacement array constructed by the values fetched with your query. Link to comment https://forums.phpfreaks.com/topic/183356-help-with-forum-quoting/#findComment-968102 Share on other sites More sharing options...
play_ Posted November 30, 2009 Share Posted November 30, 2009 I must ask: You'll have people type @<user id> as opposed to @<username> ? Link to comment https://forums.phpfreaks.com/topic/183356-help-with-forum-quoting/#findComment-968134 Share on other sites More sharing options...
DJCMBear Posted November 30, 2009 Author Share Posted November 30, 2009 Thanks for the replys and I'll try doing the code you said cags. and play_ im using id number becuase its a student id number which everyone knows and dont use the personal database id number so it dont matter if other users know the id numbers as they type @123456. Link to comment https://forums.phpfreaks.com/topic/183356-help-with-forum-quoting/#findComment-968164 Share on other sites More sharing options...
DJCMBear Posted November 30, 2009 Author Share Posted November 30, 2009 I have finaly done it and it works like a dream Thanks for everyone's help Link to comment https://forums.phpfreaks.com/topic/183356-help-with-forum-quoting/#findComment-968501 Share on other sites More sharing options...
cags Posted November 30, 2009 Share Posted November 30, 2009 Don't forget to to mark the topic solved (button in bottom left hand corner). Link to comment https://forums.phpfreaks.com/topic/183356-help-with-forum-quoting/#findComment-968507 Share on other sites More sharing options...
DJCMBear Posted November 30, 2009 Author Share Posted November 30, 2009 I have done it already Link to comment https://forums.phpfreaks.com/topic/183356-help-with-forum-quoting/#findComment-968529 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.