Search code examples
phpurldetection

Detection - the title of the URL and the URL


How to detect, if there is any URL in the text and title it has (if any)?

If there is one, then it should change the URL:

from: http://stackoverflow.com

into:

<detected:url="http://stackoverflow.com"/>

I need also to retrieve titles from external links like this example:

<title:http://stackoverflow.com/="the actual title from the stackoverflow"/>

Solution

  • This is for single URL case:

    $url = "http://www.stackoverflow.com/";
    
    $check_result = get_detected_and_title( $url );
    
    
    function get_detected_and_title( $url )
    {
        $detected = '<detected:url="'.$url.'"/>';
        $title = '';
        $tmp_html = file_get_contents( $url );
        preg_match('/<title>(.*)<\/title>/', $tmp_html, $res);
        $title = '<title:'.$url.'="'.$res[1].'"/>';
    
        return array( $detected, $title );
    }
    

    Actually, after looking through SO's pages, I think this is more close to what you looking for. Although it needs some adjustment: How to mimic StackOverflow Auto-Link Behavior