What are the potential pitfalls of using regular expressions in PHP to clean up text, and how can multiple iterations of text processing affect the outcome?

Potential pitfalls of using regular expressions in PHP to clean up text include inefficient patterns that can slow down processing, difficulty in handling complex text structures, and the risk of unintended side effects if not carefully crafted. Multiple iterations of text processing can compound these issues and lead to unexpected outcomes.

// Example code snippet demonstrating the use of regular expressions with PHP's preg_replace function to clean up text
$text = "Hello,   World! This is a  test  string.";
$cleaned_text = preg_replace('/\s+/', ' ', $text); // Remove extra spaces
$cleaned_text = preg_replace('/[^a-zA-Z0-9\s]/', '', $cleaned_text); // Remove non-alphanumeric characters
echo $cleaned_text;