In PHP replacing text between TAGS & URI information in Title Tag

Hi,

what I am trying to do in PHP, is to replace the Title. I need some of the URL information inside aswell depending on the domain.

The title is always different so I need to store it in a variable, put the url info like described below in front of it.
Here is an example how it should look like:

<title>This is my site</title>

should be changed into:

for

www.cool.com
<title>Cool - This is my site</title>

for

www.very-cool.com
<title>Very Cool - This is my site</title>

for

www.thisis.very-cool.com
 <title>Thisis Very Cool - This is my site</title>

I tried to start with this, but it fails somehow:

$text = '<title> bahh blahh </title>';
$pattern = "/<title\b[^>]*>(.*?)<\/title>/ims";
$content = "<title>newtitle</title>";
echo preg_replace($pattern, $content, $text);

The resukted endtitle tag looks like this:

<\/title>

if I remove "\" I get an error because it is missing...

so I now work with strpos like this:

$start = stripos($result, "<title>")+ strlen("<title>");
$length = strpos($result, "</title>", $start) - $start; 
$text = substr($result, $start, $length);

Is't there a more elegant way to change the title, but I need to store the content in between.