search
HomeCMS TutorialDEDECMSHow to use dedecms string interception function

How to use dedecms string interception function

#How to use dedecms string interception function?

dedecms cn_substr_utf8 string interception function discussion

Recommended study: 梦weavercms

This article I saw on phpsir, The main thing is that there seems to be some problems with the cn_substr_utf8 function of dedecms. Friends who study dedecms can take a look

The cn_substr_utf8 function in dedecms is like this

The code is as follows:

/**
* utf-8中文截取,单字节截取模式
*
* @access public
* @param string $str 需要截取的字符串
* @param int $slen 截取的长度
* @param int $startdd 开始标记处
* @return string
*/
if ( ! function_exists('cn_substr_utf8'))
{
function cn_substr_utf8($str, $length, $start=0)
{
if(strlen($str) < $start+1)
{
return &#39;&#39;;
}
preg_match_all("/./su", $str, $ar);
$str = &#39;&#39;;
$tstr = &#39;&#39;;</p> <p> //为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取
for($i=0; isset($ar[0][$i]); $i++)
{
if(strlen($tstr) < $start)
{
$tstr .= $ar[0][$i];
}
else
{
if(strlen($str) < $length + strlen($ar[0][$i]) )
{
$str .= $ar[0][$i];
}
else
{
break;
}
}
}
return $str;
}
}

The code is as follows:

if(strlen($str) < $length + strlen($ar[0][$i]) )

One line may cause an extra character after interception. You can consider changing it to

The code is as follows:

if(strlen($str) < $length + strlen($ar[0][$i]) -1 )

The test code is as follows

The code is as follows:

$f = "你好fasdfa你fasdf#e#";
$pos = strpos($f,'#e#');
var_dump($pos);
var_dump(cn_substr_utf8($f,$pos));
var_dump(cn_substr_utf82($f,$pos));

function cn_substr($str, $slen, $startdd=0) { global $cfg_soft_lang; if($cfg_soft_lang=='utf-8') { return cn_substr_utf8($str, $slen, $startdd); } $restr = ''; $c = ''; $str_len = strlen($str); if($str_len < $startdd+1) { return ''; } if($str_len < $startdd + $slen || $slen==0) { $slen = $str_len - $startdd; } $enddd = $startdd + $slen - 1; for($i=0;$i<$str_len;$i++) { if($startdd==0) { $restr .= $c; } else if($i > $startdd) { $restr .= $c; }

if(ord($str[$i])>0x80) { if($str_len>$i+1) { $c = $str[$i].$str[$i+1]; } $i++; } else { $c = $str[$i]; }

if($i >= $enddd) { if(strlen($restr)+strlen($c)>$slen) { break; } else { $restr .= $c; break; } } } return $restr; }

function cn_substr_utf8($str, $length, $start=0) { if(strlen($str) < $start+1) { return ''; } preg_match_all("/./su", $str, $ar);

$str = ''; $tstr = '';

//为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取 for($i=0; isset($ar[0][$i]); $i++) { if(strlen($tstr) < $start) {

$tstr .= $ar[0][$i]; } else {

if(strlen($str) < $length + strlen($ar[0][$i]) ) {

$str .= $ar[0][$i]; } else {

break; } } } return $str; }

function cn_substr_utf82($str, $length, $start=0) { if(strlen($str) < $start+1) { return ''; } preg_match_all("/./su", $str, $ar);

$str = ''; $tstr = '';

//为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取 for($i=0; isset($ar[0][$i]); $i++) { if(strlen($tstr) < $start) {

$tstr .= $ar[0][$i]; } else {

if(strlen($str) < $length + strlen($ar[0][$i]) -1 ) // phpsir 加了 -1 {

$str .= $ar[0][$i]; } else {

break; } } } return $str; }

The above is the detailed content of How to use dedecms string interception function. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)