#assuming input is in $string $string=join('',map( m![<>\\/]! ? "#".char($_).";" : $_, split(//, $string))); # ^^^^^^^ These are trouble characters