What are the considerations when adding links to individual strings in PHP output based on file names?

When adding links to individual strings in PHP output based on file names, it is important to ensure that the links are valid and point to the correct location. One way to achieve this is by using a combination of string manipulation functions to extract the file name from the string and then construct the appropriate link based on that file name.

<?php
// Example string containing file names
$string = "Hello, please check out my resume (resume.pdf) and portfolio (portfolio.pdf)";

// Regular expression to match file names enclosed in parentheses
preg_match_all('/\((.*?)\)/', $string, $matches);

// Loop through matched file names and construct links
foreach ($matches[1] as $file) {
    $link = '<a href="' . $file . '">' . $file . '</a>';
    $string = str_replace('(' . $file . ')', $link, $string);
}

echo $string;
?>