网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 网站设计 > PHP
User Name
Password
 
FAQ Members List Calendar Cheap Glasses

Reply
 
Thread Tools Display Modes
  #1   IP: 49.87.137.233
Old 2014-11-17, 11:20 AM
Quay Quay is offline
初级会员
 
Join Date: 2013-07-01
Posts: 1
Quay 现在声名狼藉
Default 直接用SMARTY中的truncate方法截取中文及英文(utf8)

新建一个名为“modifier.truncate_utf8.php”的PHP文件,复制到"smarty/plugins"这个目录下面.
Code:
<?php
/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */


/**
 * Smarty truncate modifier plugin
 *
 * Type:     modifier<br>
 * Name:     truncate<br>
 * Purpose:  Truncate a string to a certain length if necessary,
 *           optionally splitting in the middle of a word, and
 *           appending the $etc string or inserting $etc into the middle.
 * @link http://smarty.php.net/manual/en/language.modifier.truncate.php
 *          truncate (Smarty online manual)
 * @author   Monte Ohrt <monte at ohrt dot com>
 * @param string
 * @param integer
 * @param string
 * @param boolean
 * @param boolean
 * @return string
 */
function smarty_modifier_truncate_utf8($string, $length = 80, $etc = '...',
$break_words = false, $middle = false)
{
    $patten   = '/<img.*?>/i '; 
    $string= preg_replace($patten , ' ' ,$string); 
	if ($length == 0)
	return '';

	if (strlen($string) > $length) {
		$i = 0;
		$newword = '';
		while($i<$length){
			if(ord($string[$i]) > 224){
				$newword .= $string[$i].$string[$i+1].$string[$i+2];
				$i = $i +3;
			}else if(ord($string[$i] > 192)){
				$newword .= $string[$i].$string[$i+1];
				$i = $i +2;
			}
			else{
				$newword .= $string[$i];
				$i++;
			}
		}
		return $newword . $etc;
	} else {
		return $string;
	}
}

/* vim: set expandtab: */

?>
这个方法只是在一个开源商城的iestore程序中用到了,如果不好用,可能有漏下的地方,请在研究一下iestore这个商城的tools/smarty/plugins/modifier.truncate.php这个文件
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 11:07 AM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.