You need to do:
header("Content-Type:text/plain");
before you output it.
This tells the browser to output the content as a pure text format and not as html.
Of course you could also do it like this:
$srtnew = str_replace(" ",htmlentities("<SP>"),$string);
If you have many tags you should just set the header if you only want to output that code in order to let your users copy them.
More information about setting headers in PHP: http://www.php.net/manual/de/function.header.php