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
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?

Link to comment
Share on other sites

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>"

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.