简体   繁体   中英

how can I do preg_replace?

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>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM