1

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 Answers 2

5

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
}
Sign up to request clarification or add additional context in comments.

Comments

0

You seem to be breaking the text into words separated by spaces, and then matching each word against a regular expression. This is very time consuming indeed. A faster way to do this is to perform the regular expression search on the entire text and then iterate over it's results.

preg_match_all($regex, $text, $result, PREG_PATTERN_ORDER);
foreach($result[0] as $jt){
    //do what you normally do with $jt
}

2 Comments

this ok but i want to return text that not contain link. how to do this with a Regex
You can do preg_replace_all($regex, "", $text) which will delete all patterns that match $regex. However this is a little dangerous as it may delete more than you were counting on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.