Smarty replace text with double quotes
Question
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?
Solution
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.
OTHER TIPS
You do know ‘em’ units in HTML width/height attributes aren't valid, right? That's CSS only.
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.