I had to do this a few weeks ago as my hard drive corrupted..
I used diskAid to rip my music from iphone.. you don't need to be jailbroken either, but why wouldn't you want to be?
DiskAid is free.. Avail here
DiskAid - Transfer Files betwen your Computer and your iPhone or iPod Touch. Disk Mode for your iPhone and iPod Touch.
your music on your phone is stored in var/mobile/Media/iTunes_Control/Music & folder names are F1, F2 etc/..
Then i used tagscanner to automatically rename all of the tracks from the random letters that itunes changes them to (like HFKSMF) back to the original tracknames, it will update everything automatically, saves hours!
Tagscanner from here. Free to download..
TagScanner - Free software downloads and reviews - CNET Download.com
Then open your fresh install of itunes and import the music.. job done -- hope it helps