Search code examples
phphtmltagsstr-replacemailto

<br> vs. <BR> with str_replace in PHP


I'm trying to do a str_replace on a text block like below that is in variable $who:

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

I'm using str_replace below to remove the <br> and <BR> tags:

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

My problem is that the end result does not get rid of
tags. The output looks like this:

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

I didn't think capitalization would be a problem for str_replace(). I've tried pre_replace() as well and used both double and single quotes. I'm inserting this into the body of mailto link and html tags need to go. Any suggestions? Thank you in advance.

EDIT: I am trying to remove all br and BR tags from the string in variable $who. I need to replace them with %0D%0A. This is because the resulting string is being inserted into the body of a mailto. Here is the full code:

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

Solution

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

    The i option let the preg_replace() function work case-insensitive, so it should replace <br> and <BR>. To use this in an mailto: link a possible way could be to use urlencode().