与PHP中的str_replace相对于

时间:2017-06-19 21:00:22

标签: php html tags str-replace mailto

我试图在下面的文本块上执行str_replace,该文件块位于变量$ who:

Acme Corporation
<BR><br>Accounting Dept
<BR><br>123 Sesame St.
<BR><br>New York, NY 10021
<BR><br>(123)456-7890

我使用下面的str_replace删除了<br><BR>代码:

$who = str_replace("<br>","%0D%0A",html_entity_decode($who));
$who = str_replace("<BR>","%0D%0A",html_entity_decode($who));

我的问题是最终结果没有摆脱
标签。输出如下:

    Acme Corporation
<BR>Accounting Dept
<BR>123 Sesame St.
<BR>New York, NY 10021
<BR>(123)456-7890

我并不认为大写会成为str_replace()的问题。我也试过了pre_replace()并使用了双引号和单引号。我将其插入到mailto链接的正文中,并且需要使用html标记。有什么建议?先感谢您。

编辑:我正在尝试从变量$ who中的字符串中删除所有br和BR标记。我需要用%0D%0A替换它们。这是因为生成的字符串被插入到mailto的主体中。这是完整的代码:

//GetValues where CCDLookUps were unnecessary
$who = CCDLookUp("data", "template", "template_name = 'poscc_header'", $db);
$terms = $payment1->pterms->GetValue(); 
$buyer = $users->uName->GetValue();
$comp = $users->company->GetValue();
$bEmail = $users->uMail->GetValue();    
$totSold = $invoices1->total->GetValue();
$tot = $invoices1->total->GetValue();
$payamt = $payment1->amount->GetValue();
$seq = $payment1->seq->GetValue();

//correct Money output
$totSold = money_format('$%i', $totSold);   
$price = money_format('$%i', $price);   
$tax = money_format('$%i', $tax);
$ship = money_format('$%i', $ship); 
$tot = money_format('$%i', $tot);   
$payamt = money_format('$%i', $payamt);

//builds header and header template     

$who = str_replace("<br>","%0D%0A",html_entity_decode($who));
//$who = str_replace("<BR>",'%0D%0A',html_entity_decode($who));
$bodymsg = $who;


//main body     
$bodymsg .= "%0D%0A%0D%0AConfirmation: ".$order_id."%0D%0AOrder Date: ".$odate."%0D%0ADelivery: ".$deltype;
$bodymsg .= "%0D%0ATerms: ".$terms."%0D%0ASeller ID: ".$sellerID."%0D%0ABuyer: ".$buyer."%0D%0A             ".$comp;
$bodymsg .= "%0D%0A%0D%0A";
$bodymsg .= "%0D%0ATotal Items Sold: ".$totSold."%0D%0AQty: ".$qty."%0D%0AProduct: ".$prod."%0D%0APrice: ".$price;
$bodymsg .= "%0D%0ATax: ".$tax."%0D%0AShipping: ".$ship."%0D%0ATotal: ".$tot."%0D%0A%0D%0APayment Amount: ".$payamt;
$bodymsg .= "%0D%0ASeq: ".$seq."%0D%0A%0D%0A%0D%0ACustomer Copy%0D%0A%0D%0ANo Refunds/No Exchanges%0D%0A";
$bodymsg .= "Thank you";

//place into message and set to link        
$mlto = "mailto:".$bEmail."&subject=Receipt%20For%20Order%23%20".$order_id."&body=".$bodymsg;
$Page->eLink->SetValue($mlto);

2 个答案:

答案 0 :(得分:0)

<?php
    $result = preg_replace("~<br>~i", "\r\n", $who);
?>

i选项让preg_replace()函数不区分大小写,因此应该替换<br><BR>。 要在mailto:链接中使用此功能,可能的方法是使用urlencode()

答案 1 :(得分:0)

我从您的问题中了解到您想要用新行替换
标签,因此您必须使用\ n引用代码中的新行,它将如下所示

 <?php
        $who="Acme Corporation
        <BR><br>Accounting Dept
        <BR><br>123 Sesame St.
        <BR><br>New York, NY 10021
        <BR><br>(123)456-7890";
        $who = str_replace("<br>","\n",html_entity_decode($who));
        echo $who;
        ?>