[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] 3 Questions and a Possible



Joseph, can't help you on your first and last question, but...

>2. I'm trying to take a string of characters, look for "special" characters 
>and transliterate those special characters to &#XXX; strings for html. Any 
>ideas on a clean way to do this. I have one solution that's (in my opinion 
>ugly) but works; see following code:
>
>        @a = unpack('C*', $string);
>        for($i=0; $i < @a; $i++){
>          $num = $a[$i];
>          if($num < 32 || $num == 38 || $num == 60 || $num == 62 || 
>          ($num >90 && $num < 97) || $num > 122){
>            $a[$i] = "&$a[$i]\;";
>           }else{
>             $a[$i] = chr($a[$i]);
>           }
>         }
>         $string = join('', @a);

Whoa.  This must be syzygy;  I wrote code to do _exactly_ that not more 
than five minutes ago!  Very weird.

My solution:

$string =~ s/&/&amp;/g;
$string =~ s/"/&quot;/g;
$string =~ s/</&lt;/g;
$string =~ s/>/&gt;/g;
$string =~ s/([\200-\377])/"&#".ord($1).";"/ge;

I suppose it could all be done in the last line, by using
([\040\046\074\076\200-\377]) instead of ([\200-\377]), but I prefer to 
see those four tags spelled out.

Hm, are 91-96 and 123-127 considered special as well?  I guess I should 
reread the HTML spec...  :-)


--
 Jamie McCarthy          http://www.absence.prismatix.com/jamie/
 jamie@voyager.net        Co-Webmaster of http://www.nizkor.org/