Extract URL from string

前端 未结 5 646
野的像风
野的像风 2020-11-27 06:41

I\'m trying to find a reliable solution to extract a url from a string of characters. I have a site where users answer questions and in the source box, where they enter thei

5条回答
  •  Happy的楠姐
    2020-11-27 07:00

    $string = preg_replace('/https?:\/\/[^\s"<>]+/', '$0', $string);
    

    It only matches http/https, but that's really the only protocol you want to turn into a link. If you want others, you can change it like this:

    $string = preg_replace('/(https?|ssh|ftp):\/\/[^\s"]+/', '$0', $string);
    

提交回复
热议问题