Extract URLs from text in PHP

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

    If the text you extract the URLs from is user-submitted and you're going to display the result as links anywhere, you have to be very, VERY careful to avoid XSS vulnerabilities, most prominently "javascript:" protocol URLs, but also malformed URLs that might trick your regexp and/or the displaying browser into executing them as Javascript URLs. At the very least, you should accept only URLs that start with "http", "https" or "ftp".

    There's also a blog entry by Jeff where he describes some other problems with extracting URLs.

提交回复
热议问题