jaikob Posted October 4, 2009 Share Posted October 4, 2009 I'm trying to craft a function to pull @ replies from a string, and I somehow cant get str_replace to work. Here is my code: <?php function convertToLink($user) { $profile = "/profile/"; $query = "SELECT * FROM users WHERE username = '$user' LIMIT 0,1"; $result = mysql_query($query); $row_user = mysql_fetch_assoc($result); $row_Total = mysql_num_rows($result); return "@<a href=\"".$profile.$row_user['username']."\">".$user."</a>"; mysql_free_result($result); } function parseShout($input) { $val = array(); $tags = array(); $shout = $input; preg_match_all("#@[a-z0-9]+#i", $input, $tags); foreach ($tags as $val) { for ($i=0;$i<count($val); $i++) { // Store our changes into an array $val[$i] = convertToLink(substr($val[$i], 1, 20000000)); }} // $val holds all new replacements // $tags holds old values return str_replace($tags, $val, $shout); } ?> an example usage would be: <?php echo parseShout("Hello @jaikob how are you today?"); ?> But when I execute that, It just returns the string without replacing anything. if I do a print_r($val); and print_r($tags); they print the correct array/values. Any clues as to what I'm doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/176459-solved-twitter-replies-help/ Share on other sites More sharing options...
ProXy_ Posted October 4, 2009 Share Posted October 4, 2009 ok, just so i have a better understanding, your pulling @names from mysql and then you want to display them as @<a href="/profile/name"> correct? Quote Link to comment https://forums.phpfreaks.com/topic/176459-solved-twitter-replies-help/#findComment-930187 Share on other sites More sharing options...
jaikob Posted October 4, 2009 Author Share Posted October 4, 2009 Actually... The convertToLink would be this. mysql is not needed. function convertToLink($user) { $profile = "/profile/$user"; return "@<a href=\"".$profile."\">".$user."</a>"; } Quote Link to comment https://forums.phpfreaks.com/topic/176459-solved-twitter-replies-help/#findComment-930190 Share on other sites More sharing options...
ProXy_ Posted October 4, 2009 Share Posted October 4, 2009 ok, do you want only 1 result. or all results ? Quote Link to comment https://forums.phpfreaks.com/topic/176459-solved-twitter-replies-help/#findComment-930191 Share on other sites More sharing options...
jaikob Posted October 4, 2009 Author Share Posted October 4, 2009 ok, do you want only 1 result. or all results ? Both the results and the replacements are stored into an array. Yes I would like to replace all @relies with their convertToLink counterparts Quote Link to comment https://forums.phpfreaks.com/topic/176459-solved-twitter-replies-help/#findComment-930194 Share on other sites More sharing options...
ProXy_ Posted October 4, 2009 Share Posted October 4, 2009 Ok, so i was toying around with it. and this is what i'm getting so far: http://ju.nu/test/test.php Thats your code. but i've modified it a little bit. to look like this: <?php function parseShout($input) { $shout = $input; preg_match_all("#@[a-z0-9]+#i", $input, $tags); foreach ($tags as $val) { for ($i=0;$i<count($val); $i++) { // Store our changes into an array $val[$i]=str_replace("@", "", $val[$i]); echo "@<a href='/profiles/$val[$i]'>$val[$i]</a> <br>"; }} // $val holds all new replacements // $tags holds old values } echo parseShout("Hello @jaikob how are you today? @george "); ?> Does this help you out? Quote Link to comment https://forums.phpfreaks.com/topic/176459-solved-twitter-replies-help/#findComment-930197 Share on other sites More sharing options...
jaikob Posted October 4, 2009 Author Share Posted October 4, 2009 Yes, it is a leg further , but I need the original string to be there too. Thanks a lot for helping me out. so it would be like "Hello @<a href="profile/jaikob">jaikob</a> how are you today? @<a href="profile/george">george</a>" Quote Link to comment https://forums.phpfreaks.com/topic/176459-solved-twitter-replies-help/#findComment-930198 Share on other sites More sharing options...
ProXy_ Posted October 4, 2009 Share Posted October 4, 2009 Ok, once again i have Updated http://ju.nu/test/test.php so you can see what it looks like now. here is the result: function parseShout($input) { $profile = "/profile/"; $shout = $input; preg_match_all("#@[a-z0-9]+#i", $input, $tags); foreach ($tags as $val) { for ($i=0;$i<count($val); $i++) { // Store our changes into an array $val[$i]=str_replace("@", "", $val[$i]); echo "@<a href='/profiles/$val[$i]'>$val[$i]</a> how are you today?<br>"; }} // $val holds all new replacements // $tags holds old values } echo parseShout("Hello @jaikob! how are you @george today?"); ?> by this i'm assuming that your always gonna say how are you today? to your users. if you have different speech intro's to different users we can figure something else out. Notice how i took the ConvertLink function out? Well i did this mainly because you can call your mysql in the parseShout function and get the same result. Quote Link to comment https://forums.phpfreaks.com/topic/176459-solved-twitter-replies-help/#findComment-930201 Share on other sites More sharing options...
jaikob Posted October 4, 2009 Author Share Posted October 4, 2009 I think we are now getting too complicated. A user posts a message, and he says "hello @jaikob how are you?" I want the @usernames to be linked. I don't want to seperate all @usernames and have a different string for each one, I have an original string, and I want to replace all @usernames in that string with @<a href="profile/username">username</a> So If I passed parseShout("hello @jaikob how are you?"); parseShout will return: "hello @<a href="profile/jaikob">jaikob</a> how are you?" Thanks for your assistance, I just keep having issues finding and replacing in a string basically. Quote Link to comment https://forums.phpfreaks.com/topic/176459-solved-twitter-replies-help/#findComment-930203 Share on other sites More sharing options...
cags Posted October 4, 2009 Share Posted October 4, 2009 How about? echo preg_replace("~@(\w+)?~", "@<a href=\"profile/$1\">$1</a>", $src); Quote Link to comment https://forums.phpfreaks.com/topic/176459-solved-twitter-replies-help/#findComment-930207 Share on other sites More sharing options...
jaikob Posted October 4, 2009 Author Share Posted October 4, 2009 YESS Thank You all! Quote Link to comment https://forums.phpfreaks.com/topic/176459-solved-twitter-replies-help/#findComment-930209 Share on other sites More sharing options...
ProXy_ Posted October 4, 2009 Share Posted October 4, 2009 ah, good call...i don't know why i didn't pick that up earlier Quote Link to comment https://forums.phpfreaks.com/topic/176459-solved-twitter-replies-help/#findComment-930212 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.