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.youkuaiyun.com/codersay 魂牵梦萦苛夺需要佣兵赶不上';
preg_match_all('/http:\/\/[A-Za-z0-9_.\/]+(\s?)/', $html,$arr);//获取url以空格结尾
print_r($arr);php提取纯文本中的链接地址
最新推荐文章于 2024-12-19 20:01:51 发布
本文介绍了一个使用PHP和正则表达式提取文本中链接地址的脚本,包括ANSI和UTF-8编程环境下的实现方式。脚本实例演示了如何在网页文本中查找并输出链接地址。
1491

被折叠的 条评论
为什么被折叠?



