Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Twitter API在文本末尾隐藏URL_Php_Regex_Twitter - Fatal编程技术网

Php Twitter API在文本末尾隐藏URL

Php Twitter API在文本末尾隐藏URL,php,regex,twitter,Php,Regex,Twitter,我正在使用twitterapi从一个集合中检索数据。问题是返回的文本或全文字符串在tweet的末尾附加了一个短URL 例如: 我们在一起可以做的比我们任何一个人都多。t、 co/cf1lVHw0i8 我有以下内容来替换和转换链接和hashtag function linkify_tweet($tweet) { //Convert urls to <a> links $tweet = preg_replace("/([\w]+\:\/

我正在使用twitterapi从一个集合中检索数据。问题是返回的文本或全文字符串在tweet的末尾附加了一个短URL

例如: 我们在一起可以做的比我们任何一个人都多。t、 co/cf1lVHw0i8

我有以下内容来替换和转换链接和hashtag

    function linkify_tweet($tweet) {
    
      //Convert urls to <a> links
      $tweet = preg_replace("/([\w]+\:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/", "<a target=\"_blank\" href=\"$1\">$1</a>", $tweet);
    
      //Convert hashtags to twitter searches in <a> links
      $tweet = preg_replace("/#([A-Za-z0-9\/\.]*)/", "<a target=\"_new\" href=\"http://twitter.com/search?q=$1\">#$1</a>", $tweet);
    
      //Convert attags to twitter profiles in <a> links
      $tweet = preg_replace("/@([A-Za-z0-9\/\.]*)/", "<a href=\"http://www.twitter.com/$1\">@$1</a>", $tweet);
    
      //Remove links
      $pattern = "/[a-zA-Z]*[:\/\/]*[A-Za-z0-9\-_]+\.+[A-Za-z0-9\.\/%&=\?\-_]+/i";
      $replacement = "";
      $tweet=preg_replace($pattern, $replacement, $tweet);

      return $tweet;
    
    }
最后一个正则表达式删除了所有链接,但我只想删除结尾的shortenURL,如果文本中存在任何其他链接,则保留它

有什么想法吗


谢谢

假设Twitter的缩短链接始终遵循您的示例中提供的格式,您只需使用

解决方案

t\.co\/\S+\S*$

注意,您需要执行两个匹配,首先确定是否存在非t.co链接。如果有,则删除具有上述模式的t.co链接。

在与url匹配的模式中添加$。