0

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 listed.

<!-- Model # Start -->
<p style='text-align: right;'>16736</p>
<!-- Model # End -->
1
  • 1
    What is the purpose of the concatenating of empty strings? $products[''.$row[0].'']. Trying to get something like (string) cast? Commented Dec 18, 2010 at 1:18

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I think I jumped the gun to quick. This code ... $products[''.$row[0].''][2] = preg_replace("/(.*<!-- Model # Start -->).*(<!-- Model # End -->.*)/im", "$1$2", $products[''.$row[0].''][2]); ... matches in the Rubular but does not work.
actually I just needed a /ims , whatever the s does made it work multi-line. Thanks, for the help!
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.