php提取纯文本中的链接地址
header("Content-type=text/html;charset=gb2312"); /** * @author http://weibo.com/codersay * 功能是将纯文本中的链接地址给提取出来,前提是链接地址是以空格 或者 中文字符分隔 */ $strtest = "http://list.tmall.com/search_product.htm?spm=3.1000473.a2223p1.4.naAM6o&active=1&from=sn_1_cat&area_code=330100&search_condition=7&vmarket=0&style=g&sort=s&n=60&s=0&cat=50041288#J_crumbs 中文字符yyg"; echo "原始链接地址为:".$strtest."<br /><br />"; //(1) ANSI编程环境下: ####$pregstr = "/([".chr(0xb0)."-".chr(0xf7)."][".chr(0xa1)."-".chr(0xfe)."])+/i"; $pregstr = "/(http://[A-Za-z0-9_#?.&=/]+)([".chr(0xb0)."-".chr(0xf7)."][".chr(0xa1)."-".chr(0xfe)."])?(s)?/i"; if(preg_match($pregstr,$strtest,$matchArray)){ echo $matchArray[1];//以空格或者中文仅接着链接地址,提取链接地址 //print_r($matchArray); } echo "<br /><hr />"; //output:中文字符 //(2) Utf-8编程环境下: ###$pregstr = "/([x{4e00}-x{9fa5}])+/u"; $pregstr = "/(http://[A-Za-z0-9_#?.&=/]+)([x{4e00}-x{9fa5}])?(s)?/u"; if(preg_match($pregstr,$strtest,$matchArray)){ echo $matchArray[1];//以空格或者中文仅接着链接地址,提取链接地址 //print_r($matchArray); } //output:中文字符 exit(); // set source file name and path $source = "toi200686.txt"; $html = "标题作者魂牵梦萦魂牵梦萦基本面魂牵梦萦http://blog.csdn.net/codersay 魂牵梦萦苛夺需要佣兵赶不上"; preg_match_all("/http://[A-Za-z0-9_./]+(s?)/", $html,$arr);//获取url以空格结尾 print_r($arr);
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: PHP经验集锦
- 下一篇: PHP项目设计基本步骤
copyright © 2008-2019 亿联网络 版权所有 备案号:粤ICP备14031511号-2