如何在 php 中的 post 中找到主题标签和标签以及 url?

How do I find hashtag and tag and url in post in php?

我在一个项目上工作,我正在尝试通过 php 在 post 中添加检测话题标签、话题标签和 url 的能力。 我写了代码,但它只有 returns 链接 我怎样才能 return 所有三个链接在一起?

 public function shortcut_links()
    {

    
    $post_text=   $this->post_text;
      $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
      $tag_text= "/@(\w+)/";
      $hashtag_text= "/#(\w+)/";
    
    if(preg_match_all($reg_exUrl, $post_text, $urls)) {
         foreach($urls[0] as $url){
            
               $post_text = preg_replace($reg_exUrl,'<a href='.$url.' >'. app("bitly")->getUrl($url).'</a>', $post_text);

          }
         
        return $post_text;

    }  elseif(preg_match_all($tag_text, $post_text, $urls)) {
        // make the urls hyper links,
        foreach($urls[0] as $url){
            
               $post_text = preg_replace($tag_text,'<a href='.$url.' >'.$url.'</a>', $post_text);

          }
          return $post_text;

     } 
   elseif(preg_match_all($hashtag_text, $post_text, $urls)) {
         foreach($urls[0] as $url){
            
               $post_text = preg_replace($hashtag_text,'<a href='.$url.' >'.$url.'</a>', $post_text);

          }
          return $post_text;

    } 
  else {
        return $post_text; 
    }
    return $post_text; 


    }

由于使用 if... elseif... 逻辑,您的代码目前只能替换一种类型的项目。这意味着如果一个匹配 - 其余的将不会完成。

如果您只是对每种类型的项目使用 if...,它将查找所有类型。

您还可以删除所有多余的 return 语句,这样它所做的最后一件事就是 return 替换所有匹配项...

if(preg_match_all($reg_exUrl, $post_text, $urls)) {
     foreach($urls[0] as $url){
        
           $post_text = preg_replace($reg_exUrl,'<a href='.$url.' >'. app("bitly")->getUrl($url).'</a>', $post_text);

      }
}  
if(preg_match_all($tag_text, $post_text, $urls)) {
    // make the urls hyper links,
    foreach($urls[0] as $url){
        
           $post_text = preg_replace($tag_text,'<a href='.$url.' >'.$url.'</a>', $post_text);

    }

 } 
if(preg_match_all($hashtag_text, $post_text, $urls)) {
     foreach($urls[0] as $url){
        
           $post_text = preg_replace($hashtag_text,'<a href='.$url.' >'.$url.'</a>', $post_text);

      }
}

return $post_text;

此外,如果您的所有替换都相同,则考虑将各种正则表达式放入一个数组中并对其进行循环(或将它们组合成 1 个正则表达式)。

更新: 您的示例的一些缩写代码 $post_text= "一条提及@dummyuser 和@dummyuser0 并标有#dummytag 和#dummytag0 的虚拟消息你可以访问这个link: https://protothema.gr/technology/article/1175500/…";

$reg_exUrl = "/((http|https|ftp|ftps\):\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?))/";
$tag_text= "/@(\w+)/";
$hashtag_text= "/#(\w+)/";

$post_text = preg_replace($reg_exUrl,"<a href=\"\">" . app("bitly")->getUrl($url) .'</a>', $post_text);
$post_text = preg_replace($tag_text,"<a href=\"\"></a>", $post_text);
$post_text = preg_replace($hashtag_text,"<a href=\"\"></a>", $post_text);


echo $post_text;

给...

A dummy message mentioning @ dummyuser and <a href="dummyuser0">dummyuser0</a> and tagged with <a href="dummytag">dummytag</a> and <a href="dummytag0">dummytag0</a> you can visit this link: <a href="http">URL</a>s://protothema.gr/technology/article/1175500/…

拿起 URL 效果不是很好,但您可以阅读 What is the best regular expression to check if a string is a valid URL?