0

I'm looking to change some regex I'm using to cater for HTML comment tags instead of CSS.

I've built a preg replace function that can edit between comments in a stylesheet like so:

$find = array();
    $find[0] = "/(\/\*bgColor\*\/).*?(\/\*\/bgColor\*\/)/i";
    $find[1] = "/(\/\*titleColor\*\/).*?(\/\*\/titleColor\*\/)/i";
$replace = array();
    $replace[0] = "\\1 background-color: $bgColor; \\2";
    $replace[1] = "\\1 color: $titleColor; \\2";
$result = preg_replace($find, $replace, $file);

The way this works is demonstrated below:

/*titleColor*/ color: #000; /*/titleColor*/

Any content between the two CSS tags will be replaced.

I'm looking to replicate this idea but with html like so:

<!--titleColor--> content_to_replace  <!--/titleColor-->

What do I need to change in the preg_replace in order for this to work correctly with html comment tags?

1
  • An example of some before/after HTML comment would make it easier for us to understand, I suppose. Commented Feb 28, 2012 at 21:54

1 Answer 1

1
$find = array();
    $find[0] = '/(<!--titleColor-->)(.*?)(<!--\/titleColor-->)/i';
$replace = array();
    $replace[0] = '$1' . 'replacement' . '$3';
// I prefer enclosing the current inner value with patantheses
// in case you might wanna change or use it inside the $replace value.
Sign up to request clarification or add additional context in comments.

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.