PHP preg_replace() no longer matching, when it once did

Recently I ran into a case where a PHP preg_replace was no longer matching on a condition. After nearly pulling my hair out, I discovered that the problem was that the subject now had newline characters in it.

In this particular instance, I was replacing part of a SQL statement, so my preg_replace looked something like:

$insertSQL = preg_replace('/SELECT (.*) FROM/', "INSERT INTO...", $sql);

The above should replace everything between the "SELECT" and the "FROM clause. As it turns out, there were newline characters after each database column listed, and this cause the PHP preg_match to fail. After stripping the newline characters, the code was matching and replacing as expected.

I'm just glad we caught this during our test cycle before our next big release, as this is part of a feature that is used quite frequently by the sales team.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.