Ranjit Panigrahi
Ranjit Panigrahi

Reputation: 49

String Replacement Within Tags

This is my string I'm getting from an array field:

[email_content] => 

Dear {NAME},

{USR}Thank You For Connecting With "Company" .Your Username is {$username}.{/USR}



{PWD}Thank You For Connecting With "Company".Please Click on the below Link to Resest Yor Password.{/PWD}
 {$password_link}

Thanks & Regards
ABCD Team.

Sometimes I want to omit only the {USR} and {/USR} tags of the whole string with null leaving the content between them intact and sometimes I want to replace the whole content within {USR} and {/USR} including both the opening and closing {USR} tags leaving other string contents untouched.

I tried many string functions but it just made my coding lengthy.

Upvotes: 0

Views: 41

Answers (1)

Toto
Toto

Reputation: 91518

How about:

$replacement = '$0';
if ($remove_tag_only) {
    $replacement = '$1';
} else if ($remove_all) {
    $replacement = '';
}
$string = preg_replace('~\{USR\}(.+?)\{/USR\}~s', $replacement, $string);

Upvotes: 3

Related Questions