【问题标题】:How to remove empty CSS property by PHP?如何通过 PHP 删除空的 CSS 属性?
【发布时间】:2020-07-25 10:31:20
【问题描述】:

我有一个这样的字符串

.header{
font-family:;
background-color:red;
color:blue;
line-height:;
}

.footer{
background-color:green;
color:;
}

我想像这样转换这个字符串

.header{
background-color:red;
color:blue;
}
.footer{
background-color:green;
}

我怎样才能做到这一点?

【问题讨论】:

  • 1.你从哪里收到这样的输入? 2. CSS中忽略了空属性,那么这个练习的目的是什么?
  • 感谢您的快速回复。我正在动态生成内部 CSS。像这样显示到浏览器中。 prnt.sc/ry0lw2 所以我想摆脱没有任何价值的财产。每次显示动态 CSS 值(prnt.sc/ry0off)时,我都可以实现此添加条件。但我想知道是否还有其他方便的方法。

标签: php string replace str-replace


【解决方案1】:

试试:

preg_replace('/.*:\s*;/i', '', $string);

虽然,输出也将包括color:blue; 而不仅仅是background-color:red; for .header

【讨论】:

  • 是的,你是对的。我很抱歉这个错误。代码工作正常。非常感谢。
  • 没问题,很高兴为您提供帮助:)
猜你喜欢
  • 2014-01-21
  • 2011-03-21
  • 2017-08-03
  • 1970-01-01
  • 2012-03-31
  • 2021-11-27
  • 1970-01-01
  • 2011-01-02
  • 1970-01-01
相关资源
最近更新 更多