How can regex be used to replace text after a specific character in a URL in PHP?

To replace text after a specific character in a URL using regex in PHP, you can use the preg_replace function. You can define a regex pattern to match the text after the specific character in the URL and then use preg_replace to replace it with the desired text.

$url = "https://www.example.com/page?id=12345";
$new_id = "67890";

$new_url = preg_replace('/(?<=id=)\d+/', $new_id, $url);

echo $new_url;