牛骨文教育服务平台(让学习变的简单)
博文笔记

php提取纯文本中的链接地址

创建时间:2013-11-14 投稿人: 浏览次数:134
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);


声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。