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 关于大棚监测的pcb板设计
  • ¥20 sim800c模块 at指令及平台
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计