How can URLs in a string be replaced with exceptions in PHP?
To replace URLs in a string with exceptions in PHP, you can use regular expressions to identify URLs and then replace them with a placeholder or exception message. This can be useful when you want to mask or remove URLs from a string for security or privacy reasons.
$string = "Check out my website at https://www.example.com for more information.";
$exceptionMessage = "URLs are not allowed in this context.";
$pattern = '/(https?:\/\/(?:www\.)?[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,}(\/\S*)?)/';
$replacement = $exceptionMessage;
$newString = preg_replace($pattern, $replacement, $string);
echo $newString;