How to replace post image url before posting using api?

My first suggestion is to output a WXR formatted file from phpNuke if possible. I’m not familiar with it, however I’m pretty sure it’s possible on any platform with database access. If you aren’t familiar with WXR, install a sandbox WP environment, create a few posts, categorize them, create and assign parent pages, upload image attachments, and then create an export under Tools -> Export. This file will give you all the syntax you need to use the standard WordPress Importer plugin.

If that is not an option, I would just import all the posts initially using the method you’ve created, and then build out a function to migrate the images separately. It would make for a cleaner operation in my humble opinion.

For the cleanup script, I would simply build out my own function based on media_sideload_image since it does not return the desired attachment ID that you need.

function foo_import_image($url, $post_id, $desc, $set_as_featured = true){

    $tmp = download_url($url);
    preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $url, $matches);
    $file_array['name'] = basename($matches[0]);
    $file_array['tmp_name'] = $tmp;

    if ( is_wp_error( $tmp ) ) {
        @unlink($file_array['tmp_name']);
        $file_array['tmp_name'] = '';
    }

    $id = media_handle_sideload( $file_array, $post_id, $desc );

    if($set_as_featured)
        update_post_meta($post_id, '_post_thumbnail', $id);

    return $id;
}

Here’s how I would implement the preceding function:

//do a query to get the post_content fields
//use_preg_match all to identify if the content contains an image

$old_url="http://foo.com/images/bar.jpg"; //this would be matched content
$description = 'Foo Bar Image';
$id = foo_import_image($old_url, $post_id, $description, false);
$new_url = wp_get_attachment_url($id)

global $wpdb;
$wpdb->query("UPDATE $wpdb->posts SET post_content = replace(post_content, '$old_url', '$new_url') WHERE ID = '$post_id' LIMIT 1");

My final suggestion is to create a separate dev environment to test this so as not to mess up a good import batch. Whenever I do this type of operation, I do a full DB dump before I start importing masses of images into the DB.