douyun8674 2017-10-04 21:51
浏览 77
已采纳

使用str_replace()(函数中包含的所有内容)调用PHP函数的替代方法(或正确方法)是什么?

My question is basically whether or not what I am trying to do is even possible. I am either doing something incorrectly, or I am dreaming too big... (Insert unprofessional php designer disclaimer here).

I am creating an automatic emailing system. I have the layout of my email stored in a text file. Information that I wanted to be written dynamically is represented by what I am calling tags (i.e. [DATE], [CUSTNAME], [MESSAGE]...).

When an email is created and delivered, variables are passed to a function from my index page, I simply include the text file while replacing my tags with dynamic content. This works beautifully for me, up until I wanted to beef up content in the newsletter. Now I want to add in a section that is called from an independent function. This caused my Email function to actually display the content from the called function 3 times right there rather than complete the function of shooting the email and redirect as necessary.

My code (contained inside a function) before updating (working for me):

$EmailBodyTEXT = addslashes($EmailBody);
$EmailBody = 
str_replace('[YEAR]', date(Y),
str_replace('[SUBJECT]', $EmailSubject, 
str_replace('[DATE]', date('l, F j, Y'), 
str_replace('[MESSAGE]', $EmailBody, file_get_contents($url . 'newsletter_BLANK.txt')))));

My code after updating (fail):

$EmailBodyTEXT = addslashes($EmailBody);
$EmailBody = 
str_replace('[YEAR]', date(Y),
str_replace('[SUBJECT]', $EmailSubject, 
str_replace('[DATE]', date('l, F j, Y'), 
str_replace('[MESSAGE]', $EmailBody,
str_replace('[PHOTOSTREAM]', PhotoStream(CUST), file_get_contents($url . 'newsletter_BLANK.txt'))))));

For what it is worth, I tried to 'load' the second function in a $variable and replaced the function call with the variable, exact same result, as I anticipated.

How should I call this function to not 'execute' on the spot, but continue with the process of the containing function, which would pass this content into the email in the process of creation and delivery? Am I simply incorrect in thinking there is a way to load a function from the str_replace process? Is there a more obvious way of trying to do this (basically a mail merge) function?

Please let me know your thoughts and if more code is needed from me.

Here is the PhotoStream():

function PhotoStream($DispTo) {
global $url, $uri, $urp, $locurl;

if(empty($DispTo)) { echo 'unconfigured'; continue; } else {

if($DispTo == 'CUST') { $DispTo = urlencode('$redacted'); } else { $DispTo = '$redacted'; }

}

if($getPics = mysql_query("SELECT pid, px, title FROM photos ORDER BY pubdate DESC LIMIT 6")) {

echo '<fieldset><div id="wrapNL">';

while($gPics = mysql_fetch_array($getPics)) {

$pid = $gPics{'pid'};
$pex = $gPics{'px'};
$ptl = $gPics{'title'};

echo '<div id="pthumbs"><a href="' . $uri . $DispTo . '&PID=' . $pid . $locurl . '">';
echo '<img src="' . $urp . 'uploads/' . $pid . '.' . $pex . '" alt="' . $ptl . '" title="' . $ptl . '">';
echo '</a></div>';

}

echo '</div></fieldset>';

}

}
  • 写回答

1条回答 默认 最新

  • doudouxuqh198138 2017-10-04 21:58
    关注

    I think you are overcomplicating this.

    If you want to replace multiple alues within a string you can uae the strtr function. It takes 2 arguments, the first is the string to replace the valies into and the second is an associate array of keys representing replacements and there values representing the value to replace them with.

    Try something similar to the following:

    <?php
    
    $emailBody = strtr($emailBodyRaw, array(
        '[TOKEN]' => 'token value',
        '[ANOTHER]' => 'another value',
        //More replacements here
    ));
    

    Your more complex replacements can be done outside of this snippet and substitued in as a single variable.


    EDIT: You will want to update your PhotoStream function to return a value rather than echo it aswell. You can always echo whats returned if required somewhere else within your app


    EDIT 2: Capturing PhotoStream echo output in a variable

    Using a method known as output buffering you can use your PhotoStream function in its current state by doing something similar to the following:

    <?php
    
    ob_start();
    PhotoStream($DispTo)
    $photo = ob_get_clean();
    ob_end_clean()
    
    $emailBody = strtr($emailBodyRaw, array(
        '[PHOTO]' => $photo
        //More replacements here
    ));
    

    Doing this you capture what PhotoStream echo's and store it in a variable.

    To use the function directly in the replacement array you will have to refactor it to return the html rather than echo it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error