Extract URLs from text in PHP

后端 未结 14 2211
野趣味
野趣味 2020-11-22 13:29

I have this text:

$string = \"this is my friend\'s website http://example.com I think it is coll\";

How can I extract the link into another

14条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-22 13:58

    You could try this to find the link and revise the link (add the href link).

    $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    
    // The Text you want to filter for urls
    $text = "The text you want to filter goes here. http://example.com";
    
    if(preg_match($reg_exUrl, $text, $url)) {
    
           echo preg_replace($reg_exUrl, "{$url[0]} ", $text);
    
    } else {
    
           echo "No url in the text";
    
    }
    

    refer here: http://php.net/manual/en/function.preg-match.php

提交回复
热议问题