PEEL Shopping
Open source ecommerce : PEEL Shopping
shared.mb_wordwrap.php
Go to the documentation of this file.
1 <?php
9 if(!function_exists('smarty_mb_wordwrap')) {
10 
22  function smarty_mb_wordwrap($str, $width=75, $break="\n", $cut=false)
23  {
24  // break words into tokens using white space as a delimiter
25  $tokens = preg_split('!(\s)!S' . Smarty::$_UTF8_MODIFIER, $str, -1, PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE);
26  $length = 0;
27  $t = '';
28  $_previous = false;
29 
30  foreach ($tokens as $_token) {
31  $token_length = mb_strlen($_token, Smarty::$_CHARSET);
32  $_tokens = array($_token);
33  if ($token_length > $width) {
34  // remove last space
35  $t = mb_substr($t, 0, -1, Smarty::$_CHARSET);
36  $_previous = false;
37  $length = 0;
38 
39  if ($cut) {
40  $_tokens = preg_split('!(.{' . $width . '})!S' . Smarty::$_UTF8_MODIFIER, $_token, -1, PREG_SPLIT_NO_EMPTY + PREG_SPLIT_DELIM_CAPTURE);
41  // broken words go on a new line
42  $t .= $break;
43  }
44  }
45 
46  foreach ($_tokens as $token) {
47  $_space = !!preg_match('!^\s$!S' . Smarty::$_UTF8_MODIFIER, $token);
48  $token_length = mb_strlen($token, Smarty::$_CHARSET);
49  $length += $token_length;
50 
51  if ($length > $width) {
52  // remove space before inserted break
53  if ($_previous && $token_length < $width) {
54  $t = mb_substr($t, 0, -1, Smarty::$_CHARSET);
55  }
56 
57  // add the break before the token
58  $t .= $break;
59  $length = $token_length;
60 
61  // skip space after inserting a break
62  if ($_space) {
63  $length = 0;
64  continue;
65  }
66  } else if ($token == "\n") {
67  // hard break must reset counters
68  $_previous = 0;
69  $length = 0;
70  } else {
71  // remember if we had a space or not
72  $_previous = $_space;
73  }
74  // add the token
75  $t .= $token;
76  }
77  }
78 
79  return $t;
80  }
81 
82 }
83 ?>
static $_UTF8_MODIFIER
Flag denoting if PCRE should run in UTF-8 mode.
static $_CHARSET
The character set to adhere to (e.g.

This documentation for Open ecommerce PEEL Shopping and PEEL.fr has been generated by Doxygen on Thu Oct 15 2015 14:41:16 - Peel ecommerce is a product of Agence web Advisto SAS. All rights reserved.