-2

I'm trying to get data from a site but there are some problems. I want to change the expression begining with <div class="phraserec" and ending with </div></div> how can I do it?

$source = "<div class="gwblock" id="1936036"><div class="phraserec"bla bla...</div></div>"
$output = preg_replace('/'. preg_quote('<div class="phraserec" '.'(.*?)'.'</div></div>','/') .'/', '</div>' , $ll);

I wanted output;

<div class="gwblock" id="1936036"><div class="phraserec"bla bla...</div>
  • [Parsing HTML with regex is a hard job](https://stackoverflow.com/a/4234491/372239) HTML and regex are not good friends. Use a parser, it is simpler, faster and much more maintainable. See: http://php.net/manual/en/class.domdocument.php – Toto Oct 18 '19 at 18:47
  • `preg_quote()` is disabling the special meaning of `(.*?)`. – Barmar Oct 18 '19 at 18:53
  • I'm not sure I understand why you're trying to do. Just get rid of one of the two `` at the end? – Barmar Oct 18 '19 at 18:56
  • @Toto It seems like he's trying to either parse or create invalid HTML, so DOMDocument won't work. – Barmar Oct 18 '19 at 18:57
  • @Barmar yes if "phraserec" into "gwblock" i wanted only on "/div" sorry for my bad english. – Semih Gürler Oct 18 '19 at 19:07

1 Answers1

0

There's no reason to call preg_quote(), and it's preventing (.*?) from being treated as a pattern. And instead of putting the capture group just around .*?, you can put it around everything before the second </div>.

$output = preg_replace('#(<div class="phraserec".*?</div>)</div>#', '$1', $source);

preg_quote() is needed when you want to turn dynamic input into a literal value in a regular expression.

Barmar
  • 741,623
  • 53
  • 500
  • 612