douwei1904 2011-08-19 08:02
浏览 148

php函数从字符串中提取链接

i want to extract href link from text or string. i write a little function to do that but this is slow when a string to transform is large. My code is

function spy_linkIntoString_Format($text) {
    global $inc_lang; $lang = $inc_lang['tlang_media'];
    $it = explode(' ' ,$text);
    $result = '';
    foreach($it as $jt) {
        $a = trim($jt);
        if(preg_match('/((?:[\w\d]+\:\/\/)?(?:[\w\-\d]+\.)+[\w\-\d]+(?:\/[\w\-\d]+)*(?:\/|\.[\w\-\d]+)?(?:\?[\w\-\d]+\=[\w\-\d]+\&?)?(?:\#[\w\-\d]*)?)/', $jt)) {
            $pros_lis = str_replace('www.','',$jt);
            $pros_lis = (strpos($pros_lis, 'http://') === false ? 'http://'. $pros_lis : $pros_lis);
            $urlregx = parse_url($pros_lis);
            $host_name = (!empty($urlregx['host']) ? $urlregx['host'] : '.com');
            if($host_name == 'youtube.com') {
                $string_v = $urlregx['query']; parse_str($string_v, $outs); $stID = $outs['v'];
                $result .= '<a title="Youtube video" coplay="'.$stID.'" cotype="1" class="media_spy_vr5" href="#"><span class="link_media"></span>'.$lang['vtype_youtube'].'</a> ';
            } elseif($host_name == 'vimeo.com') {
                $path_s = $urlregx['path']; $patplode = explode("/", $path_s); $stID = $patplode[1];
                $result .= '<a title="Vimeo video" coplay="'.$stID.'" cotype="2" class="media_spy_vr5" href="#"><span class="link_media"></span>'.$lang['vtype_vimeo'].'</a> ';
            } elseif($host_name == 'travspy.com') {
                $result .= '<a href="'.$jt.'" title="'.$pros_lis.'" ><span class="jkt_445 jkt_3256 c8_big_corner"></span>'.$pros_lis.'</a> ';
            } else {
                $result .= '<a href="'.$jt.'" title="'.$pros_lis.'" rel="nofollow" target="_blank"><span class="jkt_445 c8_big_corner"></span>'.$pros_lis.'</a> ';
            }
        } else {
            $result .= $jt.' ';
        }
    }
    return trim($result);/**/
}

Can i do this run fast?

  • 写回答

2条回答 默认 最新

  • duange051858 2011-08-19 08:09
    关注

    You should rewrite this to use preg_match_allinstead of splitting the text into words (i.e. drop the explode).

    $regex = '/\b((?:[\w\d]+\:\/\/)?(?:[\w\-\d]+\.)+[\w\-\d]+(?:\/[\w\-\d]+)*(?:\/|\.[\w\-\d]+)?(?:\?[\w\-\d]+\=[\w\-\d]+\&?)?(?:\#[\w\-\d]*)?)\b/';
    preg_match_all($regex, $text, $matches, PREG_SET_ORDER);
    foreach ($matches as $match) {
        $url = $match[0];
        // your link generator
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?