dongmi5015 2011-11-24 01:09
浏览 518

PHP检查字符串是否包含没有http://的URL

What is the best way to find if a string contains a url?

I'm writing a chatbox and need to deny the posting of urls in it...

if(preg_match('/[a-zA-Z]+:\/\/[0-9a-zA-Z;.\/?:@=_#&%~,+$]+/', $clean_message, $matches))
{
die('INVALID!');
}

seems to do the trick for urls that contain http:// but i need to also be able to deny urls such as youtube.com and if an ipaddress is posted etc..

I need to keep the chatbox clean from users spamming urls!

  • 写回答

1条回答 默认 最新

  • duanjia9577 2011-11-24 01:15
    关注

    Probably the best you can do is something like:

    /[\w\d\.]+\.(com|org|ca|net|uk)/
    

    and add in all other common suffixes. Doing something like:

    /[\w\d\.]+\.[\w\d]{1,3}/
    

    might be too generic but would catch pretty much anything that looked like a URL.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据