Bug #18194 Updated: preg_replace not matching \n properly

From: Date: Sat, 06 Jul 2002 08:50:29 +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-13294@lists.php.net to get a copy of this message
ID: 18194 Updated by: sander@php.net Reported By: magus@the-magi.com -Status: Open +Status: Bogus Bug Type: *Regular Expressions Operating System: Windows XP PHP Version: 4.2.1 New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Thanks for pointing this out, msopacua@idg.nl! Previous Comments: ------------------------------------------------------------------------ [2002-07-06 04:42:01] msopacua@idg.nl Please read the documentation: http://www.php.net/manual/en/pcre.pattern.modifiers.php Modifier 's'. ------------------------------------------------------------------------ [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 (#13294) next »