As mentioned elsewhere, str_replace will do what you are specifically looking for, but...
I'd be more worried about profile.php?user=John' DROP DATABASE--
Don't build queries like this. EVER. See SQL Injection for one reason why. Take a look at this article for the right way to do it.
Oh, and a comic to use as a memory aid to reinforce that you should NEVER do this.
EDIT: In response to your response (you're better off editing your original question so that it's obvious that you are clarifying your question). If you have the user 'John Johnson' stored in the database, but you want to access him with the URL profile.php?user=John_Johnson
, you need to reverse the replacement you are doing:
$user = str_replace('_', ' ', $_GET['user']);
$user = mysql_escape_string($user);
$query = mysql_query("SELECT * FROM users WHERE username = '$user'");
// finns inte användaren så skriver vi ut ett felmeddelande
if (!mysql_num_rows($query)) exit('<p>The user you are looking for appears to be missing.</p>');
This will take profile.php?user=John_Johnson
and produce the sql query: SELECT * FROM users WHERE username = 'John Johnson'
The sample code you replied with would take profile.php?user=John Johnson
and produce the sql query: SELECT * FROM users WHERE username = 'John_Johnson'
which I suspect is the opposite of what you want.
But again, I'd strongly recommend looking into prepared statements. mysql_escape_string
is really a stop-gap measure. All it takes is forgetting to use it once and you've opened up your site to hacking.