PHP解析urlmailto和twitter™s用户名和参数

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP解析urlmailto和twitter™s用户名和参数相关的知识,希望对你有一定的参考价值。

This small function receive a text as input and returns an html text with links if the source text contains urls (http://www… but also ftp://… and every other protocol), emails, twitter’s usernames (with @ at the beginning) and also twitter tags (with # at the beginning).
  1. function parse_twitter($t) {
  2. // link URLs
  3. $t = " ".preg_replace( "/(([[:alnum:]]+://)|www.)([^[:space:]]*)".
  4. "([[:alnum:]#?/&=])/i", "<a href="\1\3\4" target="_blank">".
  5. "\1\3\4</a>", $t);
  6.  
  7. // link mailtos
  8. $t = preg_replace( "/(([a-z0-9_]|\-|\.)[email protected]([^[:space:]]*)".
  9. "([[:alnum:]-]))/i", "<a href="mailto:\1">\1</a>", $t);
  10.  
  11. //link twitter users
  12. $t = preg_replace( "/ [email protected]([a-z0-9_]*) ?/i", " <a href="http://twitter.com/\1" target="_blank">@\1</a> ", $t);
  13.  
  14. //link twitter arguments
  15. $t = preg_replace( "/ +#([a-z0-9_]*) ?/i", " <a href="http://twitter.com/search?q=%23\1" target="_blank">#\1</a> ", $t);
  16.  
  17. // truncates long urls that can cause display problems (optional)
  18. $t = preg_replace("/>(([[:alnum:]]+://)|www.)([^[:space:]]".
  19. "{30,40})([^[:space:]]*)([^[:space:]]{10,20})([[:alnum:]#?/&=])".
  20. "</", ">\3...\5\6<", $t);
  21. return trim($t);
  22. }

以上是关于PHP解析urlmailto和twitter™s用户名和参数的主要内容,如果未能解决你的问题,请参考以下文章

Twitter-打印用户的最新tweet(摘自PHP中的RSS提要和linkifies链接)

在Wordpress中创建twitter小部件函数.php文件

web网页动态分享facebook和twitter

无法使用 Twitter API、PHP 和 curl 发布图像和文本?媒体/上传和状态/更新

我可以在解析时将一个人与 twitter 和 facebook 联系起来吗?

如何使用php解析json数组并显示?