【问题标题】:Turn URLs into links when showing the latest tweet from a Twitter account with preg_replace使用 preg_replace 显示来自 Twitter 帐户的最新推文时将 URL 转换为链接
【发布时间】:2011-05-16 10:32:39
【问题描述】:

我拼凑了下面的代码块,以帮助我在我的网站上显示来自我的 Twitter 帐户的最新推文。但是,它不太正常,你能帮我调试一下最后一点吗?我正在寻找 PHP 将其转换为 HTML,并在 Twitter 用户名和链接周围使用链接标签,并使用 preg_replace 进行链接。

如果你测试这个脚本,你会发现当它在推文中呈现标准链接时会出现问题,它会将结束 标记放在a 之后太早。我确信这相对容易修复,并且可能与转义字符或其他东西有关。

我的主要代码块:

    <?php
        /** Script to pull in the latest tweet */
        $username='benpaton';
        $format = 'json';
        $tweet = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}"));
        $latestTweet = htmlentities($tweet[0]->text, ENT_QUOTES);
        $latestTweet = preg_replace('/http:\/\/([[a-z0-9_\.\-\+\&\!\#\~\,]+)/i', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet);
        $latestTweet = preg_replace('/@([a-z0-9_]+)/i', '<a href="http://twitter.com/$1" target="_blank">@$1</a>', $latestTweet);
        echo $latestTweet;
    ?>

【问题讨论】:

  • 思考:你能不能只用空格分割推文,然后将任何以“http://”开头的段转换为 URL?它比运行正则表达式便宜得多,更简单,而且我认为它同样适用于这个用例。
  • 这听起来可行。我不确定如何做到这一点,尽管@usernames 也不是以“http://” 开头

标签: php twitter preg-replace tweets


【解决方案1】:

将您的正则表达式更改为:

$latestTweet = preg_replace('/http:\/\/([a-z0-9_\.\-\+\&\!\#\~\/\,]+)/i', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet);

这对我有用。

完整代码

<?php
    /** Script to pull in the latest tweet */
    $username='benpaton';
    $format = 'json';
    $tweet = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}"));
    $latestTweet = htmlentities($tweet[0]->text, ENT_QUOTES);
    $latestTweet = preg_replace('/http:\/\/([a-z0-9_\.\-\+\&\!\#\~\/\,]+)/i', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet);
    $latestTweet = preg_replace('/@([a-z0-9_]+)/i', '<a href="http://twitter.com/$1" target="_blank">@$1</a>', $latestTweet);
    echo $latestTweet;
?>

【讨论】:

  • 当我使用插入到我的代码块中的 preg_replace 行时,$latestTweet 对我来说根本没有得到回显,这很奇怪。
  • 它发布了这样的内容@bandq @Yellowkim 看看这个:paper.li/bandq -->(作为完整链接)
  • 谢谢你,是的,它现在也可以在我的网站上运行。不知道为什么它会播放一秒钟。非常感谢。
【解决方案2】:

试试这个:

<?php
/** Script to pull in the latest tweet */
$username='benpaton';
$format = 'json';
$tweet = json_decode(file_get_contents("http://api.twitter.com/1/statuses/user_timeline/{$username}.{$format}"));
$latestTweet = htmlentities($tweet[0]->text, ENT_QUOTES);
$latestTweet = preg_replace('%http://[a-z0-9_.+&!#~/,\-]+%', '<a href="http://$1" target="_blank">http://$1</a>', $latestTweet);
$latestTweet = preg_replace('/@([a-z0-9_]+)/i', '<a href="http://twitter.com/$1" target="_blank">@$1</a>', $latestTweet);
echo $latestTweet;
?>

【讨论】:

    【解决方案3】:

    所有代码都有错误或不完整!你想做的是这样的:

    $tweets[$i]['text_html'] = htmlspecialchars($tweet['text']);
    $tweets[$i]['text_html'] = preg_replace('%(http://([a-z0-9_.+&!#~/,\-]+))%i','<a href="http://$2">$1</a>',$tweets[$i]['text_html']);
    $tweets[$i]['text_html'] = preg_replace('/@([a-z0-9_]+)/i','<a href="http://twitter.com/$1">@$1</a>',$tweets[$i]['text_html']);
    

    【讨论】:

      猜你喜欢
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 2012-07-31
      • 2023-02-15
      • 2012-11-17
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      相关资源
      最近更新 更多