drfrvbq736383 2012-09-24 12:50
浏览 79
已采纳

如何在HTML textarea输出中查找和转换使用大括号{site_name}的字符串?

I am designing a Mail Template editor in my application. I need an idea to find the occurance of specific variables to be able to convert them using preg_replace or preg_match in my code.

For example, my template looks like this: (Sample only) this is what is returned from the textarea variable.

<p>Thank you for your order at <a href="{site_url}" style="color:#ea6ea0">{site_name}</a>.</p>

In this example, I would like to replace the {site_url} with a specific variable or code from PHP since I can't parse PHP directly into the textarea.

Hope my question was clear, any help appreciated.

Edit: Is my question clear? I need to replace the {} strings using my own php code. The php code cann't be used in textarea directly. That is why i need to find a templating system that replace predefined variables {... } but convert them using php when interpreting the template.

  • 写回答

2条回答 默认 最新

  • dsfadsa08911 2012-09-24 12:52
    关注

    Do you mean something like this?

    <p>Thank you for your order at <a href="<?=$site_url?>" style="color:#ea6ea0"><?=$site_name?></a>.</p>
    

    or

    <p>Thank you for your order at <a href="<?php echo $site_url?>" style="color:#ea6ea0"><?php echo $site_name?></a>.</p>
    

    edited:

    Ahhh, do you mean something like this then:

    $html = '<p>Thank you for your order at <a href="{site_url}" style="color:#ea6ea0">{site_name}</a>.</p>';
    
    $php_variables_array = array(
      "site_url" => "http://www.google.co.uk",
      "site_name" => "Google",
    );
    
    foreach ($php_variables_array as $key => $value)
    {
      $html = str_replace("{" . $key . "}", $value, $html);
    }
    
    echo $html;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历