How can I edit html links using php?

Let's say I have a huge variable called $file
$file contains thousands of lines of html, css, javascript and more.
But, I want to be able to find all of these link and edit them.
I want to only edit the ones that don't have a domain name.
Example: I would edit this link: /folder/folder/file.css
I would change it to: http://www.domain.com/folder/folder/file.css
Is there a way I can do this with php automatically?