我怎样才能获得
my $name = 'John "FOO" Rambo';
扩展
<span title=\"The great and mighty $name\"
类似
<span title=\"The great and mighty John \"FOO\" Rambo\"</span>
而不是
<span title=\"The great and mighty John "FOO" Rambo\"
在生成页面时,我的html标记没有损坏,名称包含"
?
答案 0 :(得分:2)
正确的HTML将如下:
public static final String BETRAG_KEY = "betrag";
public static final String BETRAG_ART = "art";
public static final String UST_PROZENT = "ust";
@Override
public void onClickBerechnen(View v) {
final EditText txtBetrag = (EditText) findViewById(R.id.edt_betrag);
final float betrag = Float.parseFloat(txtBetrag.getText().toString());
boolean isNetto = true;
final RadioGroup rg = (RadioGroup) findViewById(R.id.rg_art);
switch (rg.getCheckedRadioButtonId()) {
case R.id.rb_art_netto: isNetto = true; break;
case R.id.rb_art_brutto: isNetto = false; break;
default:
}
final Spinner spinner = (Spinner) findViewById(R.id.sp_umsatzsteuer);
final int pos = spinner.getSelectedItemPosition();
final int[] prozentwerte = getResources().getIntArray(R.array.ust_werte);
final Intent intent = new Intent(this, ErgebnisActivity.class);
intent.putExtra(BETRAG_KEY, betrag);
intent.putExtra(BETRAG_ART, isNetto);
intent.putExtra(UST_PROZENT, prozentwerte);
startActivity(intent);
}
您可以使用以下方式获取它:
<span title="The great and mighty John "FOO" Rambo">...</span>
您可能应该使用模板系统。如果您使用Template-Toolkit,模板将是
use HTML::Entities qw( encode_entities );
my $html = '<span title="' . encode_entities("The great and mighty $name") . '">...</span>';
-or-
my $html = '<span title="The great and mighty ' . encode_entities($name) . '">...</span>';
答案 1 :(得分:-2)
使用"
my $name = 'John "FOO" Rambo';
$name =~ s/"/"/g;
my $html = "<span title=\"The great and mighty $name\"/>";
print $html ."\n";
澄清原始问题:
您可以使用简单的正则表达式替换引号。 例如:
my $name = 'John "FOO" Rambo';
$name =~ s/"/\\"/g;
my $html = "<span title=\"The great and mighty $name\"/>";
print $html ."\n";
此
$name =~ s/"/\\"/g;
只是说用"
替换所有\"
(但由于\
是特殊字符,因此需要将其转义为\\"
。
g
表示globally
,即字符串中的所有出现。
(即,如果您只运行$name =~ s/"/\\"/;
,那么$name
将为John \"FOO" Rambo
,但g
则为John \"FOO\" Rambo
)
但这不会产生有效的HTML