a class for handle the utf8 string

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;

	}

}

?>



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.yiiframework.com/doc/cookbook/16/

ooo, i’m so sorry that i didnt pay attention to the article, please forgive me :P