【问题标题】:Extract twitter tiny url from tweets从推文中提取推特小网址
【发布时间】:2013-12-24 03:15:08
【问题描述】:

我正在从推文中提取网址:

示例 twitts 是:

\'Whn I ws calld for ths song @VishalDadlani @5hekhar both said \"we finally hv the perfect song for u\" lulz http://t.co/6vzbyjfCGk #DramaQueen\' 


\'#FreeSunder @PMOIndia The world watches as India allows the torture of temple elephants to continue. PLEASE SHARE http://t.co/g8f1kgdasU\'
india

我想从所有此类用户推文中获取 tinyurl

我的代码:

<?php

$reg_exUrl = "/(?<![\>https?:\/\/|href=\"'])(?<http>(https?:[\/][\/]|www\.)([a-z]|[A-Z]|[0-9]|[\/.]|[~])*)/";
// The Text you want to filter for urls
$text = "Whn I ws calld for ths song lulz http://t.co/6vzbyjfCGk #DramaQueen\ ";
// Check if there is a url in the text
    if(preg_match($reg_exUrl, $text, $url))
    {
        echo preg_replace($reg_exUrl, "<a href='{$url[0]}'>{$url[0]}</a> ", $text);
    } else {       // if no urls in the text just return the text
       echo $text;
}

echo here 仅打印实际推文

这仅给出一个 url ,我如何从推文中获取所有 url?

【问题讨论】:

  • @Sim0n222:它给出了结果Whn I ws calld for ths song lulz http://t.co/6vzbyjfCGk #DramaQueen\ 对吗?结果我只想要 url
  • 我再次更新了我的答案:)

标签: php twitter


【解决方案1】:

仅打印 URL:

echo "<a href='{$url[0]}'>{$url[0]}</a>";

如果要匹配所有网址,请使用preg_match_all

if(preg_match($reg_exUrl, $text, $url)) {
    preg_match_all($reg_exUrl, $text, $urls);
    foreach ($urls[0] as $url) {
        echo "<a href='{$url}'>{$url}</a><br>";
    }
} else {
    echo $text;
}

【讨论】:

    【解决方案2】:

    将其用作正则表达式 URL:

    (?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))
    

    查看source 了解更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-21
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 2011-03-18
      • 2014-02-23
      • 2011-01-04
      • 2014-01-12
      相关资源
      最近更新 更多