Function to scan for image, attach it to post, set it as featured [duplicate]

@toscho is right, please do some research before posting your question.

Here’s one peice of the puzzle that should get the ball rolling for you, courtesy of bavotasan.com. It finds the first image in the post and displays it.

function getImage($num) {
    global $more;

    $more    = 1;
    $link    = get_permalink();
    $content = get_the_content();
    $count   = substr_count($content, '<img');
    $start   = 0;

    for ($i=1; $i <= $count; $i++) {
        $imgBeg = strpos($content, '<img', $start);
        $post   = substr($content, $imgBeg);
        $imgEnd = strpos($post, '>');

        $postOutput = substr($post, 0, $imgEnd + 1);
        $postOutput = preg_replace('/width="([0-9]*)" height="([0-9]*)"https://wordpress.stackexchange.com/", '',$postOutput);;
        $image[$i]  = $postOutput;

        $start = $imgEnd + 1;
    }
    if (stristr($image[$num],'<img')) {
        echo '<a href="' . $link . '">' . $image[$num] . "</a>";
    }
    $more = 0;
}

Good luck.

P.S. Google search time < 30 seconds