Bug #18194 Updated: preg_replace not matching \n properly

From: Date: Sat, 06 Jul 2002 08:42:01 +0000
Subject: Bug #18194 Updated: preg_replace not matching \n properly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-13291@lists.php.net to get a copy of this message
ID: 18194 Updated by: msopacua@idg.nl Reported By: magus@the-magi.com Status: Open Bug Type: *Regular Expressions Operating System: Windows XP PHP Version: 4.2.1 New Comment: Please read the documentation: http://www.php.net/manual/en/pcre.pattern.modifiers.php Modifier 's'. Previous Comments: ------------------------------------------------------------------------ [2002-07-06 02:17:52] magus@the-magi.com 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 this bug report at http://bugs.php.net/?id=18194&edit=1

« previous php.bugs (#13291) next »