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

Time:11-02

I worked on a project and I'm trying to add the ability to detect hashtags, hashtags and url in a post by php. I wrote the code but it only returns links How I can return all three together?

 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; 


    }

CodePudding user response:

Your code will currently only replace one type of item due to using if... elseif... logic. This means that if one matches - the rest won't be done.

If instead you just used if... for each type of item, it will then look for all types.

You would also remove all of the excess return statements so that the last thing it does is return the replacements of all of the matches...

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;

Also if all of your replacements were the same, then consider putting the various regex's in an array and looping over that (or combining them into 1 regex).

Update: Some abbreviated code with your example $post_text= "A dummy message mentioning @ dummyuser and @dummyuser0 and tagged with #dummytag and #dummytag0 you can visit this 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=\"$1\">" . app("bitly")->getUrl($url) .'</a>', $post_text);
$post_text = preg_replace($tag_text,"<a href=\"$1\">$1</a>", $post_text);
$post_text = preg_replace($hashtag_text,"<a href=\"$1\">$1</a>", $post_text);


echo $post_text;

gives...

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/…

Picking up the URL isn't working very well, but you can read What is the best regular expression to check if a string is a valid URL? for that.

  • Related