How to remove tags other than postal format container tags

It is difficult to describe my problem in words, I will try, for example:

str = '<p>lorem ipsum <xyz@abc.com> donor sit <br></p>';

I need to remove all tags except <xyz@abc.com>

How can we work in javascript as well as PHP?

My PHP solution:

class test {
    public function keepMailAddresses($text){
       $callBack = array($this,'_keepMailAddresses');
       return preg_replace_callback('/(<)([^0-9][a-zA-Z0-9_]*([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4})(>)/i', $callBack, $text);        
    }

    private function _keepMailAddresses($matches){
       return '&lt;'.$matches[2].'&gt;';
    }

}

$obj = new test();
echo $obj->keepMailAddresses('<p>lorem ipsum <xyz@abc.com> donor sit <br></p>');
+5
source share
2 answers

ok my solution is a bit strange, but will do the trick: D

$pagecode = '<p>lorem ipsum <xyz@abc.com> donor <abc_def.xyz@abc.com> sit <abc_def.xyz@abc.abc.com><br></p>';

// this will check if it a real email but you don't need it
/*$allowed = preg_match_all("/\<+([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})+\>/i", $pagecode, $matches);*/

$allowed = preg_match_all("/\<([_a-z0-9-\.]+)@([_a-z0-9-\.]+)\>/i", $pagecode, $matches);

$allowed = implode(" ", $matches[0]);
$output = strip_tags($pagecode, $allowed);
echo htmlentities($output);
+1
source

use below in the heading section

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

and do like this

str = '<p>lorem ipsum "<xyz@abc.com>" donor sit <br></p>';
+1
source

All Articles