【问题标题】:PHP: Replace CSS comments with other commentsPHP:用其他注释替换 CSS 注释
【发布时间】:2014-10-08 10:12:36
【问题描述】:

我有这段代码:

$csscomments = '!/\*[^*]*\*+([^/][^*]*\*+)*/!';
$sourcecode = preg_replace($csscomments, '<span class="csscomments">'.$csscomments.'</span>', $sourcecode);

这可以很好地找到 CSS cmets,但它绝对不能用我需要的东西替换它们。

例如,我需要将:/* Hello world */ 替换为&lt;span class="csscomments"&gt;/* Hello world */&lt;/span&gt;

现在我将其作为输出 !/\*[^*]*\*+([^/][^*]*\*+)*/!

我需要更改哪些内容才能获得正确的输出?谢谢!

【问题讨论】:

  • preg_replace 的工作方式如下:preg_replace($pattern, $replacement, $string); 你的模式和替换在那里,但你的字符串是空的。 (也 preg_replace 不需要赋值)

标签: php css comments preg-replace


【解决方案1】:

没有完全回答你的问题,但有点解决你甚至没有问过的问题(我认为):

preg_match($csscomments, $stringcontainingyourCSS?, $matches);
$sourcecode = array();
foreach ($matches as $match) {
    array_push($sourcecode, "<span class='csscomments'>" . $match . "</span>");
}

【讨论】:

  • 这很好,我已经考虑过了,我没有在字符串中得到匹配的文本。
猜你喜欢
  • 2010-12-18
  • 2011-10-27
  • 1970-01-01
  • 2010-10-15
  • 1970-01-01
  • 2012-07-28
  • 2010-10-07
  • 2014-06-08
  • 1970-01-01
相关资源
最近更新 更多