|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18194 preg_replace not matching \n properly
Submitted: 2002-07-06 02:17 UTC Modified: 2002-07-06 04:50 UTC
From: magus at the-magi dot com Assigned:
Status: Not a bug Package: *Regular Expressions
PHP Version: 4.2.1 OS: Windows XP
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: magus at the-magi dot com
New email:
PHP Version: OS:


 [2002-07-06 02:17 UTC] magus at the-magi dot 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>

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("\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);


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-06 04:42 UTC] msopacua at idg dot nl
Please read the documentation:

Modifier 's'.
 [2002-07-06 04:50 UTC]
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at and the instructions on how to report
a bug at

Thanks for pointing this out,!
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jul 23 20:01:31 2024 UTC