Extract URLs from text in PHP

后端 未结 14 2322
野趣味
野趣味 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:59

    URLs have a quite complex definition — you must decide what you want to capture first. A simple example capturing anything starting with http:// and https:// could be:

    preg_match_all('!https?://\S+!', $string, $matches);
    $all_urls = $matches[0];
    

    Note that this is very basic and could capture invalid URLs. I would recommend catching up on POSIX and PHP regular expressions for more complex things.

提交回复
热议问题