I can't speak to Perl syntax, but PHP allows for a function callback such as:
<?php
$string="<elem/en/t>Elements / are / good</elem/en/t>";
function doreplace($matches) {
return '<'.$matches[1].preg_replace('/[^A-Za-z]/','',$matches[2]).'>';
}
$string=preg_replace_callback('~<(/?)([^>]*)>~','doreplace',$string);
echo htmlentities($string);
?>
I assume the "invalid" characters would be anything other than [A-Za-z], it's just / characters that need to be removed:
<?php
$string="<elem/en/t>Elements / are / good</elem/en/t>";
function doreplace($matches) {
return '<'.$matches[1].preg_replace('~/~','',$matches[2]).'>';
}
$string=preg_replace_callback('~<(/?)([^>]*)>~','doreplace',$string);
echo htmlentities($string);
?>
A method not using additional capture groups:
<?php
$string="<elem/en/t>Elements / are / good</elem/en/t>";
function doreplace($matches) {
return preg_replace('~(?<!<)/~','',$matches[0]);
}
$string=preg_replace_callback('~<[^>]*>~','doreplace',$string);
echo htmlentities($string);
?>