1

I want to replace 'width' and 'height' on my site.

For example:

width="600" height="365"

I want to change the width and height, to

width="712" height="475"

I can do this with.

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

The problem is, the width and height is not always 600 and 365 they might be something else. like.

width="222" height="333"

Anything... I'm looking for something like this:

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

That replaces anything inside the width and height to '712' and '475'.

Anyone has any idea how to can be done? Thanks!

Muazam
  • 379
  • 1
  • 6
  • 15

2 Answers2

3
$replace = preg_replace('~width="\d*" height="\d*"~', 'width="712" height="475"', $replace);
Alix Axel
  • 151,645
  • 95
  • 393
  • 500
3

While you're better off setting the width and height once programmatically in your PHP, you can use a regular expression to find and replace it later if you must.

$replace = preg_replace('/width="\d+" height="\d+"/', 'width="712" height="475"', $replace);
Samir Talwar
  • 14,220
  • 3
  • 41
  • 65
  • Samir Talwar@ It works, thanks! But what's the different between this and the one above? – Muazam Apr 30 '11 at 13:45
  • @Muazam: This only replaces if both width and height have at least a number, mine replaces even if they are empty - other than that they are exactly the same. – Alix Axel Apr 30 '11 at 13:56
  • @Muazam: They're practically identical. We must have posted at the same time. – Samir Talwar Apr 30 '11 at 16:21