views:

2538

answers:

3

I have the following string in the smarty (php templating system) variable $test:

<img height="113" width="150" alt="Sunset" src="/test.jpg"/>

I want to add "em" to the height and width like this:

{$test|replace:'" w':'em" w'|replace:'" a':'em" a'}

But this doesn't work... What's the problem and the solution?

+2  A: 

my regex isn't the greatest, or i'd give you a better matcher, but maybe using what you have through the regex replace would work.

{$test|regex_replace:'/".w/':'em" w'|regex_replace:'/".a/':'em" a'}

other matchers to try

'/\".w/'
'/".*w/'
'/\".*w/'

i can't play with my smarty sites at the moment, but i'd first remove the " from the replacement value, to see if the bug is there, then remove it from the matcher and just look for height/width.

otherwise i'd do the replace in PHP if you can.

Adam
+4  A: 

You do know ‘em’ units in HTML width/height attributes aren't valid, right? That's CSS only.

bobince
yep, i found out after i managed to replace it all...Now i replace the width and height with 'style="width:150em; height:113em"'. This works alot better ;)
Joepie
A: 

With Aggiorno's Smart Search and Replace you can do it like this:

Search Pattern:

<img height="$h" width="$w" $attributes/>

Replace Pattern:

<img height="$[h]em" width="$[w]em" $attributes"/>

When you click the "Search" button, all the occurrences are highlighted before applying the replacement so you can do further checking, after that you can apply the replacement confidently.