How can you dynamically replace a specific part of a URL with user input from a form in PHP?

To dynamically replace a specific part of a URL with user input from a form in PHP, you can use the `str_replace()` function to replace the placeholder in the URL with the user input value. You can retrieve the user input from the form using `$_POST` or `$_GET` superglobals, sanitize the input to prevent any malicious code injection, and then use `str_replace()` to replace the placeholder in the URL with the sanitized user input.

<?php
// Retrieve user input from the form
$userInput = $_POST['user_input'];

// Sanitize the user input to prevent code injection
$sanitizedInput = htmlspecialchars($userInput);

// Define the URL with a placeholder to be replaced
$url = 'http://www.example.com/page/{placeholder}/';

// Replace the placeholder with the sanitized user input
$newUrl = str_replace('{placeholder}', $sanitizedInput, $url);

// Redirect to the new URL
header('Location: ' . $newUrl);
?>