Yii Framework Forum: a class for handle the utf8 string - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

a class for handle the utf8 string Rate Topic: -----

#1 User is offline   Davidhhuan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 270
  • Joined: 08-September 09

Posted 21 September 2009 - 04:48 AM

it is only a class to handle the utf8 string, maybe many people know how to deal with that case, i just want to share with each other ,:P


<?php
/*
	A class to handle the string, such as utf8 string,
	and also prvide dump() to print the string,
	
	LICENCE: BSD
	AUTHOR: DavidHHuan
*/
class HandleString
{
	public static function utf_substr($str, $len)
	{
		for($i=0;$i<$len;$i++)
		{
			$temp_str=substr($str,0,1);
			if(ord($temp_str) > 127)
			{
				$i++;
				if($i<$len)
				{
					$new_str[]=substr($str,0,3);
					$str=substr($str,3);
				}
			}
			else
			{
				$new_str[]=substr($str,0,1);
				$str=substr($str,1);
			}
		}
		return join($new_str);
	}
	
	public static function dump($vars, $label = '', $return = false)
	{
		if (ini_get('html_errors')) {
		    $content = "<pre>\n";
		    if ($label != '') {
		        $content .= "<strong>{$label} :</strong>\n";
		    }
		    $content .= htmlspecialchars(print_r($vars, true));
		    $content .= "\n</pre>\n";
		} else {
		    $content = $label . " :\n" . print_r($vars, true);
		}
		if ($return) { return $content; }
		echo $content;
		return null;
	}
}
?>

no pain, no gain...

My Blog : http://cnblogs.com/davidhhuan
My website: http://sharefamily.net/
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 21 September 2009 - 04:59 AM

PHP already has basic UTF8 support implemented. You can e.g. override all string functions to use their mb_* counterpart. Did you have a look at this cookbook article?

http://www.yiiframew...oc/cookbook/16/
0

#3 User is offline   Davidhhuan 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 270
  • Joined: 08-September 09

Posted 21 September 2009 - 05:02 AM

ooo, i'm so sorry that i didnt pay attention to the article, please forgive me :P
no pain, no gain...

My Blog : http://cnblogs.com/davidhhuan
My website: http://sharefamily.net/
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users