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 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题
  • ¥15 使用dify通过OpenAI 的API keys添加OpenAI模型时报了“Connection Error”错误