php.net |  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.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
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>
</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);
?>

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-06 04:42 UTC] msopacua at idg dot nl
Please read the documentation:
http://www.php.net/manual/en/pcre.pattern.modifiers.php

Modifier 's'.
 [2002-07-06 04:50 UTC] sander@php.net
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!
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Jul 21 16:01:25 2019 UTC