Bug #18194: preg_replace not matching \n properly

From: Date: Sat, 06 Jul 2002 06:17:53 +0000
Subject: Bug #18194: preg_replace not matching \n properly
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-13286@lists.php.net to get a copy of this message
From: magus@the-magi.com Operating system: Windows XP PHP version: 4.2.1 PHP Bug Type: *Regular Expressions Bug description: preg_replace not matching \n properly So what I was trying to do is replace lines such as "<td class=\"green\">8w</td> <td class=\"green\">32.48</td> <td class=\"green\">27.41</td> </tr>" with "</tr>". I attempted to use $instr = preg_replace('@<td class="(.*?)">8w</td>(.*?)</tr>@', '</tr>', $instr), but the (.*?) consistantly failed to match the \n's, when they should have. I eventually had to expand the \n's literally, meaning I had to trap the entire line, instead of just being able to use (.*?), so my preg_replace looked like so: $instr = preg_replace('@<td class="(.*?)">8w</td>\n<td class="(.*?)">(.*?)</td>\n<td class="(.*?)">(.*?)</td>\n</tr>@', '</tr>', $instr) This short script demonstrates the problem and the solution: <? //view the html without needing to view->source :) header("Content-Type: text/plain"); $instr = "<td class=\"green\">8w</td>\n<td class=\"green\">32.48</td>\n<td class=\"green\">27.41</td>\n</tr>"; //problem - doesn't work when it should $instr = preg_replace('@<td class="(.*?)">8w</td>(.*?)</tr>@', '</tr>', $instr); print($instr); print("\n\n"); //spacer //semi-solution - works, but shouldn't be necessary $instr = preg_replace('@<td class="(.*?)">8w</td>\n<td class="(.*?)">(.*?)</td>\n<td class="(.*?)">(.*?)</td>\n</tr>@', '</tr>', $instr); print($instr); ?> -- Edit bug report at http://bugs.php.net/?id=18194&edit=1 -- Fixed in CVS: http://bugs.php.net/fix.php?id=18194&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=18194&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=18194&r=needtrace Try newer version: http://bugs.php.net/fix.php?id=18194&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=18194&r=support Expected behavior: http://bugs.php.net/fix.php?id=18194&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=18194&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=18194&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=18194&r=globals

« previous php.bugs (#13286) next »