tags:

views:

48

answers:

1

Hi,

Can someone help me to tweak this regex??

I have this regex for twitter and if I spell a word like this R@chard, it wants to turn it in an URL. On Twitter itself it is displayed correctly.

Erasing the last line is maybe obvious, although I don't know much about forming regex expressions. Maybe there is also a smarter one for use with Twitter?

Also, I have an ajax twitter updater and it displays questionmark placeholders. What are they, and can regex get rid off those?

This is my code

function format_tweet($str) 
    {
$formatted_text = preg_replace('/(\b(www\.|http\:\/\/)\S+\b)/', "<a target='_blank' href='$1'>$1</a>", $str);
$formatted_text = preg_replace('/\#(\w+)/', "<a target='_blank' href='http://search.twitter.com/search?q=$1'&gt;#$1&lt;/a&gt;", $formatted_text);
$formatted_text = preg_replace('/\@(\w+)/', "<a target='_blank' href='http://twitter.com/$1'&gt;@$1&lt;/a&gt;", $formatted_text);
return $formatted_text;
}

Thanks, Richard

+1  A: 

Try:

$formatted_text = preg_replace('/(?:^|\s)[#](\w+)/', "<a target='_blank' href='http://search.twitter.com/search?q=$1'&gt;#$1&lt;/a&gt;", $formatted_text);
$formatted_text = preg_replace('/(?:^|\s)[@](\w+)/', "<a target='_blank' href='http://twitter.com/$1'&gt;@$1&lt;/a&gt;", $formatted_text);

Regarding the question marks, I've no idea. Maybe I'll be able to help if you post some code.

Alix Axel
thanks, I will try this one.As for the questionmarks, I can only see them in the public timelineand it use exactly the same code. I don't see them in my usertimeline, but that's because I don't post so much(weird stuff).Anyway, I don't no what it stands for. As far as smarter regex, I meant also to get rid of real questionmarks or exclamationmarks in tweets like if it is texted 100 times after a sentence?
Richard
You can try `preg_replace('~.*[!?]{2,}~', '?', $tweet);` to get rid of the question/exclamation marks.
Alix Axel
THERE IS ONLY ONE THING WRONG, It doesn't catch the second regex, because I can still see the www and it does not open in target blank?
Richard