本文整理汇总了Python中lib.drupy.DrupyPHP.mb_substr方法的典型用法代码示例。如果您正苦于以下问题:Python DrupyPHP.mb_substr方法的具体用法?Python DrupyPHP.mb_substr怎么用?Python DrupyPHP.mb_substr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lib.drupy.DrupyPHP
的用法示例。
在下文中一共展示了DrupyPHP.mb_substr方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: drupal_substr
# 需要导入模块: from lib.drupy import DrupyPHP [as 别名]
# 或者: from lib.drupy.DrupyPHP import mb_substr [as 别名]
def drupal_substr(text, start, length = None):
"""
Cut off a piece of a string based on character indices and counts+ Follows
the same behavior as PHP's own substr() function.
Note that for cutting off a string at a known character/substring
location, the usage of PHP's normal strpos/substr is safe and
much faster.
"""
if (lib_appglobals.multibyte == UNICODE_MULTIBYTE):
return (php.mb_substr(text, start) if \
(length == None) else mb_substr(text, start, length));
else:
strlen_ = strlen(text);
# Find the starting byte offset
bytes = 0;
if (start > 0):
# Count all the continuation bytes from the start until we have found
# start characters
bytes = -1; chars = -1;
while (bytes < strlen_ and chars < start):
bytes += 1;
c = ord(text[bytes]);
if (c < 0x80 or c >= 0xC0):
chars += 1;
elif (start < 0):
# Count all the continuation bytes from the end until we have found
# abs(start) characters
start = abs(start);
bytes = strlen_; chars = 0;
while (bytes > 0 and chars < start):
bytes -= 1;
c = ord(text[bytes]);
if (c < 0x80 or c >= 0xC0):
chars += 1;
istart = bytes;
# Find the ending byte offset
if (length == None):
bytes = strlen_ - 1;
elif (length > 0):
# Count all the continuation bytes from the starting index until we have
# found length + 1 characters+ Then backtrack one byte.
bytes = istart;
chars = 0;
while (bytes < strlen_ and chars < length):
bytes += 1;
c = ord(text[bytes]);
if (c < 0x80 or c >= 0xC0):
chars += 1;
bytes -= 1;
elif (length < 0):
# Count all the continuation bytes from the end until we have found
# abs(length) characters
length = abs(length);
bytes = strlen_ - 1;
chars = 0;
while (bytes >= 0 and chars < length):
c = ord(text[bytes]);
if (c < 0x80 or c >= 0xC0):
chars += 1;
bytes -= 1;
iend = bytes;
return substr(text, istart, max(0, iend - istart + 1));