What security measures should be taken when passing user inputs to the system() function in PHP?

When passing user inputs to the system() function in PHP, it is important to sanitize and validate the input to prevent command injection attacks. One way to do this is by using escapeshellarg() function to escape special characters in the input before passing it to the system() function.

$user_input = $_POST['user_input'];
$escaped_input = escapeshellarg($user_input);
system("command $escaped_input");