Smarty用双引号替换文本

时间:2022-09-15 15:23:00

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

我在smarty(php模板系统)变量$ test中有以下字符串:

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

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

我想在高度和宽度上添加“em”,如下所示:

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

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

但这不起作用......问题和解决方案是什么?

3 个解决方案

#1


2  

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.

否则我会用PHP替换,如果可以的话。

#2


4  

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

你知道HTML宽度/高度属性中的'em'单位是无效的,对吧?那只是CSS。

#3


0  

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

使用Aggiorno的智能搜索和替换,您可以这样做:

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.

当您单击“搜索”按钮时,所有实例都会在应用替换之前突出显示,以便您可以进一步检查,之后您可以放心地应用替换。

#1


2  

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.

否则我会用PHP替换,如果可以的话。

#2


4  

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

你知道HTML宽度/高度属性中的'em'单位是无效的,对吧?那只是CSS。

#3


0  

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

使用Aggiorno的智能搜索和替换,您可以这样做:

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.

当您单击“搜索”按钮时,所有实例都会在应用替换之前突出显示,以便您可以进一步检查,之后您可以放心地应用替换。