I am working with an editor that works purely with internal relative links for files which is great for 99% of what I use it for.

However, I am also using it to insert links to files within an email body and relative links don't cut the mustard.

Instead of modifying the editor, I would like to search the string from the editor and replace the relative links with external links as shown below

Replace 

```
files/something.pdf

```

With

```
https://www.someurl.com/files/something.pdf

```

I have come up with the following but I am wondering if there is a better / more efficient way to do it with PHP

```
<?php
$string = '<a href="files/something.pdf">A link</a>, some other text, <a href="files/somethingelse.pdf">A different link</a>';

preg_match_all('/<a[^>]+href=([\'"])(?<href>.+?)\1[^>]*>/i', $string, $result);

if (!empty($result)) {
    // Found a link.
    $baseUrl = 'https://www.someurl.com';
    $newUrls = array();
    $newString = '';

    foreach($result['href'] as $url) {
        $newUrls[] = $baseUrl . '/' . $url;
    }

    $newString = str_replace($result['href'], $newUrls, $string);

    echo $newString;
}
?>

```

Many thanks

Lee