【问题标题】:Replace height="***" stars替换 height="***" 星星
【发布时间】:2011-08-16 01:42:19
【问题描述】:

我想替换我网站上的“宽度”和“高度”。

例如:

width="600" height="365"

我想把宽高改成

width="712" height="475"

我可以做到这一点。

$replace =str_replace('width="600" height="365"', 'width="712" height="475"', $replace)

问题是,宽度和高度并不总是 600 和 365,它们可能是其他值。喜欢。

width="222" height="333"

任何...我正在寻找这样的东西:

$replace =str_replace('width="***" height="***"', 'width="712" height="475"', $replace)

这会将宽度和高度内的任何内容替换为“712”和“475”。

有人知道怎么做吗? 谢谢!

【问题讨论】:

  • 原始值是如何进入 html 的?
  • Gordon@ file_get_contents 来自另一个站点。谢谢。

标签: php replace size height width


【解决方案1】:

虽然您最好在 PHP 中以编程方式设置一次宽度和高度,但如果必须,您可以在以后使用正则表达式查找和替换它。

$replace = preg_replace('/width="\d+" height="\d+"/', 'width="712" height="475"', $replace);

【讨论】:

  • Samir Talwar@ 它有效,谢谢!但是这个和上面的有什么不同呢?
  • @Muazam:仅当宽度和高度都至少有一个数字时才会替换,即使它们是空的,我的也会替换 - 除了它们完全相同。
  • @Muazam:它们实际上是相同的。我们一定是同时发帖的。
【解决方案2】:
$replace = preg_replace('~width="\d*" height="\d*"~', 'width="712" height="475"', $replace);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 1970-01-01
    • 1970-01-01
    • 2013-02-23
    • 1970-01-01
    相关资源
    最近更新 更多