开发者

PHP Replace Between Tags with Regex

I am having a heck of a time replacing the data between two tags. I can't figure out the regex that will match this pattern. My tags are simply <!-- Model # Start --> and <!-- Model # End -->

Code:

        $products[''.$row[0].''][2] = preg_replace("/(<!-- Model # Start -->).*(<!-- Model # End -->)/i", "$1$2", $products[''.$row[0].''][2]);
        echo $products[''.$row[0].''][2] . "\n";

Data: $products[''.$row[0].''][2]

Economical. 7 mils thick, tough & stretchy. Each roll cellophane wrapped. UL list开发者_开发百科ed.

<!-- Model # Start -->
<p style='text-align: right;'>16736</p>
<!-- Model # End -->


Try:

/(.*<!-- Model # Start -->).*(<!-- Model # End -->.*)/im

Note the m flag for multiline matches.

In case you have multiple occurances of the tags, you could use a reluctant quantifier to get the first match, or use lookaround.

http://rubular.com/ helps for testing.


I'd match it like so...

$match = 'Model\s#\s';

preg_replace('/<!--\s?' . $match . 'Start\s?(.*?)\s?' . $match . 'End\s?-->/i', '<span>$1</span>', $row);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜