如何将带有变量的文本存储到数据库中?

发布于 2024-12-05 10:04:31 字数 640 浏览 0 评论 0原文

我想将文本/字符串存储在数据库的文本字段中。
该字符串中包含变量 $name。
当我将其从数据库中取出时,我希望在打印字符串之前用我定义的值替换该变量。

   # Variable I want to substitute #
1. $name='John';

   # needs to be read from database #
2. $txt{'hello'}="Hello ${name}, How are you?";

3. print "<tag>$txt{'hello'}</tag>";

它根据需要打印 Hello John, How are you?,但是当从数据库读取第二行时,它显示 Hello ${name}, How are you?

我发现的一些东西是:

  1. Locale::Maketext
  2. $string =~ s/(\$\w+)/$1/eeg;
  3. my $string = sprintf '向 %s 和 %s'、$foo、$bar; 打个招呼;

有人可以指导我如何去做吗?

I want to store text/string in a text field in a database.
This string has the variable $name in it.
When I pull it out of the database, I want that variable to be substituted with the value I define before I print the string.

   # Variable I want to substitute #
1. $name='John';

   # needs to be read from database #
2. $txt{'hello'}="Hello ${name}, How are you?";

3. print "<tag>$txt{'hello'}</tag>";

It prints Hello John, How are you? as required, but when 2nd line is read from database, it displays Hello ${name}, How are you?.

Some things I found are:

  1. Locale::Maketext
  2. $string =~ s/(\$\w+)/$1/eeg;
  3. my $string = sprintf 'Say hello to %s and %s', $foo, $bar;

Can someone guide me about how to go about it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断念 2024-12-12 10:04:31

你所描述的是一个模板。 CPAN 上有很多复杂程度不同的模板系统。 Text::Template模板工具包 是一些流行的工具包。您不想让您的模板访问任意变量;这是一个安全漏洞。相反,将允许他们访问的变量放入哈希中。

如果您需要的只是一个非常简单的系统,您可以执行以下操作:

sub fill_in_template
{
  my ($text, $values) = @_;
  $text =~ s/ \$\{ ( [^}\s]+ ) \} /$values->{$1}/gx;
  return $text;
}

my %txt;
my %values = (name => 'Your Name');

my $template  = 'Hello ${name}, How are you?'; # $name NOT interpolated
$txt{'hello'} = fill_in_template($template, \%values);

print "<tag>$txt{'hello'}</tag>\n";

您可以添加一些错误检查,以防模板使用未定义的字段。但如果您需要比这更复杂的东西,您最好从 CPAN 中选择现有的模板系统。

Locale::Maketext 旨在实现国际化(因此您的应用程序可以生成多种语言的输出,而无需翻译人员处理)直接代码)并且不是您正在寻找的那种模板引擎。

What you're describing is a template. There are lots of template systems on CPAN of varying degrees of complexity. Text::Template and Template Toolkit are a couple of popular ones. You don't want to let your templates access arbitrary variables; that's a security hole. Instead, put the variables they're allowed to access into a hash.

If all you need is a very simple system, you can do something like this:

sub fill_in_template
{
  my ($text, $values) = @_;
  $text =~ s/ \$\{ ( [^}\s]+ ) \} /$values->{$1}/gx;
  return $text;
}

my %txt;
my %values = (name => 'Your Name');

my $template  = 'Hello ${name}, How are you?'; # $name NOT interpolated
$txt{'hello'} = fill_in_template($template, \%values);

print "<tag>$txt{'hello'}</tag>\n";

You might add some error checking in case the template uses a field that's not defined. But if you need something more complicated than that, you're probably better off picking an existing template system from CPAN.

Locale::Maketext is intended for internationalization (so your app can produce output in multiple languages without your translators needing to work on the code directly) and is not the sort of template engine you're looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文