0

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; 


    }
Fedaa
  • 5
  • 1

1 Answers1

1

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.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • It gives me the same result. How can I putting the various regex's in an array and looping over that (or combining them into 1 regex). – Fedaa Oct 31 '21 at 09:26
  • @Fedaa can you give a sample of the data for me to test this? – Nigel Ren Oct 31 '21 at 09:42
  • Words containing @ without a space due to stack overflow site terms ..... $post_text= "A dummy message mentioning @ dummyuser and @ dummyuser0 and tagged with #dummytag and #dummytag0 you can visit this link: https://www.protothema.gr/technology/article/1175500/otto-wichterle-google-doodle-gia-ton-efeureti-ton-fakon-epafis/"; – Fedaa Oct 31 '21 at 09:47
  • @Fedaa I've added some abbreviated code with the test text and output – Nigel Ren Oct 31 '21 at 10:04