【问题标题】:<br> vs. <BR> with str_replace in PHP<br> 与 <BR> 在 PHP 中使用 str_replace
【发布时间】:2017-06-19 21:00:21
【问题描述】:

我正在尝试在变量 $who 中的如下文本块上执行 str_replace:

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

我在下面使用str_replace 删除&lt;br&gt;&lt;BR&gt; 标签:

$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);

【问题讨论】:

  • 你需要编辑这个以便我们可以看到标签
  • 在这里正常工作:rextester.com/STI84360
  • 嘿马辛!新用户名怎么样?
  • 问题是:邮件是如何处理的,为什么 HTML 标记标签会首先出现在图片中?
  • “我正在将它插入到 mailto 链接的正文中” - 再次;问题不清楚。发布您的完整代码。这使我的投票结束有效。最少的“那个”是有水的。

标签: php html tags str-replace mailto


【解决方案1】:
<?php
    $result = preg_replace("~<br>~i", "\r\n", $who);
?>

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

【讨论】:

    【解决方案2】:

    据我所知,您想用新行替换
    标记,因此您必须使用 \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;
            ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-21
      • 2013-08-25
      相关资源
      最近更新 更多