Linkify url and hashtag for linkedin Status and share api

IF you are working with linkedin api and parsing status and share items for any user or company page then this small code will help to parse the status or share text (comes from api) to 1. linkify 2. Tagging 3. Linkify hashtag
Please note that linkedin doesn’t support any native @ handle for it’s own user based but all @ handle are for twitter user.

/**
 * Linkify url and hashtag
 * 
 * @param type string $status_text 
 * @return type string
 */
function linkify_linkedin_status($status_text){

    // linkify URLs
    $status_text = preg_replace(
        '/(https?:\/\/\S+)/',
        '<a href="\1">\1</a>',
        $status_text
    );


    // linkify twitter users
    //please note that any thing tagged using @ in linkedin takes to twitter
    $status_text = preg_replace(
        '/(^|\s)@(\w+)/',
        '\1@<a target="_blank" href="http://twitter.com/\2">\2</a>',
        $status_text
    );


    // linkify hash tags
    $status_text = preg_replace(
        '/(^|\s)#(\w+)/',
        '\1#<a target="_blank" href="http://www.linkedin.com/signal/?keywords=%23\2">\2</a>',
        $status_text
    );

    return $status_text;
}

Leave a Reply

Your email address will not be published. Required fields are marked *