tags:

views:

132

answers:

4

I'm creating my own version of twitter, I have no idea how to get my back end php script to pick up the @membername within the entered text. Including multiple @membername's for example @billy @joseph, @tyrone,@kesha message

or

@billy hit up @tyrone he's bugging @kesha about the money you owe him.

Any scripts of use on how I can accomplish this?

+8  A: 

What about using a regex and preg_match_all, like this :

$str = "Including multiple @membername's for example @billy @joseph, @tyrone,@kesha message ";
if (preg_match_all('#(@\w+)#', $str, $m)) {
    var_dump($m[1]);
}


Which would give you the following output :

array
  0 => string '@membername' (length=11)
  1 => string '@billy' (length=6)
  2 => string '@joseph' (length=7)
  3 => string '@tyrone' (length=7)
  4 => string '@kesha' (length=6)


Basically, here, the pattern I used in my regex is matching :

Pascal MARTIN
Hey Pascal, I'm trying to insert this into the database, with your script I'd need to strip the @ to get the member name of who to send the message to. Help please.
Justin Phillips
I tried your way bro, I just keep getting an error on the 1st part of the script. I'm lost on how I can then insert the array into the database after the 1st part anyway.
Justin Phillips
How Do you input HTML on this site?Alright I finally got this thing to output something that's not an error using a combination of your script and another script. I just got an error with yours every time.Here Is the Script:$string = $breez;preg_match_all('#(@\w+)#',$string, $m);$hypertext = "<a href=http://www.BreezMe.com/>" . $m . "</a>";$newString = preg_replace('#(@\w+)#', $hypertext, $string);The output is what gets inserted into the database is this. <br><a href=http://www.BreezMe.com/>Array</a> <br>how do I get it to insert the members name without the @ in front of it?
Justin Phillips
Using your method, implode is the only way I can get it to show the names of the members I'm sending the message to, only problem now is it puts all the names of the members in one string. <a href=http://www.BreezMe.com/@test6,@test5,@test2,@test3>@test6,@test5,@test2,@test3</a> I've tried and tried but I can't get it to only put one in then move on to the next, it does it for all of them. Any help is appreciated.
Justin Phillips
If you do not want the `@` to be captured, put it outside of the `()`, in the pattern, which would then be : `#@(\w+)#` ;;; to iterate over the names, you can use a `foreach($m[1] as $name) {echo $name . '<br />';}` loop, a treat them one by one : one name per itaration of the loop
Pascal MARTIN
Thanks for you help bro, but All I get is an error message via ajax. It flat out won't work.
Justin Phillips
hu ; Ajax ? there must be more going on that you first posted ^^ ;;; can you show us your code and describe both what you're trying to do, and the error you're getting ? *(you can edit your question, if necessary -- or, depending on if the Ajax error is a problem distinct from the OP here, ask a new question)*
Pascal MARTIN
I really wish I know how to format on here but here's the code. if(isset($_REQUEST['breez'])){$breez=$_REQUEST['breez'];} if($memid!=""){ if($breez==""){$return['error'] = true;$return['msg'] = 'No Message.';}if ($breez!="") {$string = $breez;if (preg_match_all('#@(\w+)#',$string, $m)){foreach($m[1] as $name) { $item==$name; }}$hypertext = "<a href=http://www.BreezMe.com/$item>" . $item . "</a>";$newString = preg_replace('#(@\w+)#', $hypertext, $string);$strsql="INSERT INTO breez(fromid,toid,message,createdate) VALUES(\"$memid\",\"$toid\",\"$newString\",now())";
Justin Phillips
Finally got it bro, you are right there was some other error going on. I deleted everything and did it from scratch and it works perfectly. I'll have the site up soon to show you your handy work.
Justin Phillips
A: 

All depends on how you set it up...Can your user names have a space in it? If not, then just simply parse the message string for "@" and pick up all characters that come after until you encounter a space...that's a real simple way to get it done.

The other side of the question is what are you doing with the usernames once you do "find" them?

curtisk
WOW, I really see why Google is going down, I search all night on google for a script for this found nothing. In 5 mins I got great response on here. @Curtisk once I get the user names the message is added to the database and sent to each users timeline. @Dremation, thanks for your answer, I'll definitely give it a try.@Pascal MARTIN thanks for your answer also bro, but your way is a bit above my skill level. I'll give it a try though.
Justin Phillips
+1  A: 

I wrote a Wordpress plugin that handles this a long time ago. Here's the function that I use for this.

function convert_twitter_link($content) {
    $pattern    = '/\@([a-zA-Z0-9_]+) /';
    $replace    = '<a rel="nofollow" target="_blank" href="http://twitter.com/'.strtolower('\1').'"&gt;@\1&lt;/a&gt;';
    return preg_replace($pattern,$replace,$content);
}
Dremation
Dremation provide me with the link to that tutorial please. Just checked your blog for it.
Justin Phillips
I'm sorry there is no tutorial. You simply pass the information you want to scan for the @names and it'll return that content back with formatted @names linking to the url. In your case you'd have to change twitter.com to yourdomain.tld
Dremation
so in the case of multiple name I should run a while insert loop to insert it into the database?What I'm trying to do here is get the names from the string and insert the message into a database, so the message then appears in the appropriate users time line. I have everything else I just need this one part. This is on a standalone site not, wordpress.
Justin Phillips
I've tried this repeatedly bro it's not working.here's the script:if(isset($_REQUEST['breez'])){$breez=$_REQUEST['breez'];}function convert_twitter_link($breez) { $pattern = '/\@([a-zA-Z0-9_]+) /'; $replace = '<a rel="nofollow" target="_blank" href="http://twitter.com/'.strtolower('\1').'">@\1</a>'; $message= preg_replace($pattern,$replace,$breez);}$message comes out blank.
Justin Phillips
If thats the case then you'll want to use Pascal MARTIN code example. If you want to display them as links in your content then use my example.
Dremation
the var_dump sequence returns and error every time.I was able to use a mix of his code and another I found similar to your that converts it to a link, however it's giving me a count of the items in the array as the value instead of the names of the members in the array. if (preg_match_all('#(@\w+)#',$string, $m)){$arrays=preg_match_all('#(@\w+)#',$string, $m);$array = array($arrays);foreach($array as $item) { $m=$item;} $hypertext = "<a href=http://www.BreezMe.com/$m>" . $m . "</a>";$newString = preg_replace('#(@\w+)#', $hypertext, $string);
Justin Phillips
+1  A: 

There is a PHP library designed to conform to Twitter's standards. It might save you a lot of hassle. It supports autolinking as well as extraction for @screen_names, #hashtags, and @screen_name/lists.

http://github.com/mzsanford/twitter-text-php

abraham