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 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题