Are there any alternative functions or methods in PHP that can be used instead of str_replace for string manipulation to avoid potential errors or security vulnerabilities?

When dealing with string manipulation in PHP, it is important to consider potential security vulnerabilities such as code injection attacks. To avoid such issues, it is recommended to use the `preg_replace` function with proper regular expressions to sanitize input and prevent malicious code execution.

// Using preg_replace with a regular expression to sanitize input
$input = $_POST['input'];
$sanitized_input = preg_replace('/[^a-zA-Z0-9\s]/', '', $input);
echo $sanitized_input;