How to find #hashtags in strings using Laravel 4.1?

后端 未结 1 1752
小蘑菇
小蘑菇 2021-01-26 10:23

I am currently trying to filter through an Input string to find the single hashtags that a user wants to be displayed with his photo. However, I am currently getting inserts in

相关标签:
1条回答
  • 2021-01-26 10:56

    Try this:

    $str = $hashtag_string;
    
           preg_match_all('/#(\w+)/', $str, $matches);
    
    
           foreach ($matches[1] as $hashtag_name) {
    
           $hashtag =   Hashtag::firstOrCreate(array('hashtag' => $hashtag_name));
    
                                        }
    

    You could then, in this foreach loop, connect those hashtags to a post (or in your case a photo) or sth.

    0 讨论(0)
提交回复
热议问题