Identifying if a URL is present in a string

前端 未结 2 864
有刺的猬
有刺的猬 2021-01-27 01:23

Hi can someone help me identify if a url is present in a string using PHP?

I want to pull in a full string i.e. \"Hi please visit http://domain.com/12345 today\" and str

相关标签:
2条回答
  • 2021-01-27 02:01

    You probably want something like this:

    RegEx Guru: Detecting URLs in Text

    Read up on this and understand the trade-offs of each approach. For me, \bhttp://\S+ is totally acceptable, because I'm not interested in catering to those who would put links with spaces (or surround their links with parens) into plain text... (but I'm not very accomodating I guess)

    0 讨论(0)
  • 2021-01-27 02:16

    here's an example

    $str = file_get_contents('http://www.sitepoint.com');
    
    preg_match_all('~(https?://([-\w\.]+)+(:\d+)?(/([\w/_\.]*(\?\S+)?)?)?)~', $str, $matches);
    
    print_r($matches);
    
    0 讨论(0)
提交回复
热议问题