What are some best practices for extracting specific information from a string in PHP, such as URLs containing specific keywords?

When extracting specific information from a string in PHP, such as URLs containing specific keywords, one approach is to use regular expressions. Regular expressions allow you to define patterns that match the desired information within the string. By using functions like preg_match() or preg_match_all(), you can extract the URLs containing specific keywords from the input string.

$input_string = "Check out my website at https://example.com and https://example2.com for more information!";
$keyword = "example";

preg_match_all('/https:\/\/\S*' . $keyword . '\S*/', $input_string, $matches);

foreach ($matches[0] as $url) {
    echo $url . "\n";
}