Jump to content

[SOLVED] Twitter @ replies Help


jaikob

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/176459-solved-twitter-replies-help/
Share on other sites

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?

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.