What potential pitfalls should be considered when using regex to manipulate text in PHP?

One potential pitfall when using regex to manipulate text in PHP is the risk of encountering unexpected behavior due to the complexity of regex patterns. To avoid this, it is important to thoroughly test the regex pattern with various input strings to ensure it behaves as expected. Additionally, be mindful of the performance implications of using regex for text manipulation, as overly complex patterns can impact the efficiency of your code.

// Example of using regex to manipulate text in PHP with caution

$text = "Hello, this is a sample text with numbers 1234 and symbols !@#";
$pattern = "/[0-9]+/";

// Check if the regex pattern matches before performing manipulation
if (preg_match($pattern, $text)) {
    $manipulated_text = preg_replace($pattern, "", $text);
    echo $manipulated_text;
} else {
    echo "No numbers found in the text.";
}