Hi,

Wednesday, September 8, 2004, 9:14:53 AM, you wrote:
M> Anyone! I'm out of online resources and answers. I guess this isn't possible
M> to do...

Here is a function/hack I use to clean up text for my php-gtk editor,
I still haven't found a clean solution yet but it may help.


function convert_text(&$text,&$content){
    $count = 0;
    $content = '';
    $changed = false;
    for($i=0,$length=strlen($text);$i<$length;$i++){
      //echo ord($text[$i])."\n";
      //echo $text[$i]."\n\n";
      if(ord($text[$i]) > 193 && ord($text[$i]) < 224){
        if(ord($text[$i+1]) > 127 && ord($text[$i+1]) < 192){
          $changed = true;
          //echo "found 2 byte control code ".ord($text[$i])." ".ord($text[$i+1]);
          $str = $text[$i].$text[$i+1];
          $rep = htmlentities($str,ENT_QUOTES,'UTF-8');
          if(!empty($rep) && $str !== $rep){
            $content .= $rep;
            //echo " success-$rep-$rep2-$str\n";
          }else{
            $content .= '?';
            //echo " failed-$rep2-$str\n";
          }
          $i += 1;
        }else{
          $content .= $text[$i];
          echo "Oddball 2 byte ".ord($text[$i])." ".ord($text[$i+1])."\n";
        }
      }
      elseif(ord($text[$i]) > 223 && ord($text[$i]) < 240){ 
        if(ord($text[$i+1]) > 127 && ord($text[$i+1]) < 192){
          if(ord($text[$i+2]) > 127 && ord($text[$i+2]) < 192){
            $changed = true;
            //echo "found 3 byte control code ".ord($text[$i])." ".ord($text[$i+1])." 
".ord($text[$i+2]);
            $str = $text[$i].$text[$i+1].$text[$i+2];
            $rep = htmlentities($str,ENT_QUOTES,'UTF-8');
            if(!empty($rep) && $str !== $rep){
              $content .= $rep;
              //echo " success-$rep-\n";
            }else{
              $content .= '?';
              //echo " failed\n";
            }
            $i += 2;
          }else{
            $content .= $text[$i];
            echo "Oddball 3 byte-3 ".ord($text[$i])." ".ord($text[$i+1])." 
".ord($text[$i+2])."\n";
          }
        }else{
          $content .= $text[$i];
          echo "Oddball 3 byte-2 ".ord($text[$i])." ".ord($text[$i+1])." 
".ord($text[$i+2])."\n";
        }
      }else{
        $content .= $text[$i];
      }
    }
    return $changed;
  }
  //Usage
  $text = "String with quotes";   //original
  $content = '';            //result

  if(convert_text($text,$content)){
    echo $content;
  }else{
    echo $text;
  }

-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to