Wie finde ich Hashtag und Tag und URL im Beitrag in PHP?
Posted: 01 Jul 2025, 10:28
Ich habe an einem Projekt gearbeitet und versuche, die Fähigkeit hinzuzufügen, Hashtags, Hashtags und URL in einem Beitrag von Php zu erkennen.
Code: Select all
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,'[url=.$url.]'. app("bitly")->getUrl($url).'[/url]', $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,'[url=.$url.]'.$url.'[/url]', $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,'[url=.$url.]'.$url.'[/url]', $post_text);
}
return $post_text;
}
else {
return $post_text;
}
return $post_text;
}