-1

I am using this code to remove everything between this tag,however I can't make it happen. What am I missing here?

$str ='<tr class="main_row pc">
    <td class="details">
        <a href="./view/4135660/"><span class="p_t">Fiat Cinquecento </span></a>
        <a class="info" href="./view/4135660/">(Info)</a><br>
        <div class="attribs">16345, <span class="p_l">Blue</span>, Phone 6914576304
        </div>
    </td>
</tr>';


$str = preg_replace('#(<tr class="main_row pc">).*?(</tr>)#', '$1$2', $str);
Andy Lester
  • 91,102
  • 13
  • 100
  • 152
EnexoOnoma
  • 8,454
  • 18
  • 94
  • 179
  • 1
    Mistake #1: Using regexes to mangle HTML... don't do this. Use [DOM](http://php.net/dom) instead. – Marc B Sep 24 '13 at 17:18
  • Regex is not safe or simple to use on HTML. Put it through a DOM parser. – Matt S Sep 24 '13 at 17:19
  • what are you trying to replace? because that preg_replace is trying to find "#().*?()#" as a literal string which doesn't exist in $str... – Edward Sep 24 '13 at 17:19
  • See here: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags – d'alar'cop Sep 24 '13 at 17:22
  • @Edward I want to remove everything between this tag – EnexoOnoma Sep 24 '13 at 17:22
  • 2
    **Don't use regular expressions to parse HTML. Use a proper HTML parsing module.** You cannot reliably parse HTML with regular expressions, and you will face sorrow and frustration down the road. As soon as the HTML changes from your expectations, your code will be broken. See http://htmlparsing.com/php or [this SO thread](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) for examples of how to properly parse HTML with PHP modules that have already been written, tested and debugged. – Andy Lester Sep 24 '13 at 17:22
  • @AndyLester Ok i will not, the next time. – EnexoOnoma Sep 24 '13 at 17:24

3 Answers3

0

You need to use the s modifier so that . will match newlines:

$str = preg_replace('#(<tr class="main_row pc">).*?(</tr>)#s', '$1$2', $str);
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

While it is a bad idea like you've already been harped upon about in the comments, you need a pattern modifier. Specifically s to make . match newline characters. So:

preg_replace('#(<tr class="main_row pc">).*(</tr>)#s', '$1$2', $str);

Will do what you need, at least according to the above-posted code.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
0

You need to set the s PCRE modificator in order to make the dots in your RegEx match newlines, too.

TheWolf
  • 1,385
  • 7
  • 16