


Summary of methods for intercepting Chinese strings with PHP_PHP tutorial
Procedure 1: PHP method of intercepting Chinese strings
Since garbled characters (using substr) often appear when intercepting Chinese strings on the website homepage and vTigerCRM, today I found a better method of intercepting Chinese strings. Share it with everyone here.
Copy to ClipboardQuoted content: [www.bkjia.com]
function msubstr($str, $start, $len) {
$tmpstr = "";
$strlen = $start + $len;
for($i = 0; $i if(ord(substr($str, $i, 1)) > 0xa0) {
$tmpstr .= substr($str, $i, 2);
$ i++;
} else
$tmpstr .= substr($str, $i, 1);
}
return $tmpstr;
}
Program 2: PHP intercepts UTF-8 strings and solves the half-character problem

* PHP intercepts UTF-8 strings to solve the half-character problem.
* English and numbers (half-width) are 1 byte (8 bits), Chinese (full-width) are 3 bytes
* @return The string taken out, when $len is less than or equal to 0, the entire character will be returned String
* @param $str Source string
* $len The length of the left substring
**************************** *****************************************/
function utf_substr($str,$len)
{
for($i=0;$i{
$temp_str=substr($str,0,1);
if(ord($temp_str) > 127)
{
$i++;
if($i{
$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);
}
?>
php utf-8 string interception

function cutstr($string, $length) {
preg_match_all("/[x01-x7f]|[xc2-xdf][x80-xbf]|xe0[xa0-xbf][x80-xbf ]|[xe1-xef][x80-xbf][x80-xbf]|xf0[x90-xbf][x80-xbf][x80-xbf]|[xf1-xf7][x80-xbf][x80-xbf] [x80-xbf]/", $string, $info);
for($i=0; $i
$j = ord($info[0][$i]) > 127 ? $j + 2 : $j + 1;
if ($j > $ length - 3) {
return $wordscut." ...";
}
}
return join('', $info[0]);
}
$ string="242432 Opposition is 456 committed on the broad embassy local 7890";
for($i=0;$i
echo cutstr($string ,$i)."
";
}
?>
Interception UTF-8 string function
In order to support multiple languages, the strings in the database may be saved as UTF-8 encoding. During website development, you may need to use PHP to intercept part of the string. In order to avoid garbled characters, write the following UTF-8 string interception function
For the principle of utf-8, please see UTF-8 FAQ
UTF-8 encoded characters may be from 1~ It consists of 3 bytes. The specific number can be judged from the first byte. (Theoretically it may be longer, but here it is assumed that it does not exceed 3 bytes)
The first byte is greater than 224, it and the 2 bytes after it form a UTF-8 character
The first If a byte is greater than 192 and less than 224, it and the 1 byte after it form a UTF-8 character
. Otherwise, the first byte itself is an English character (including numbers and a small part of punctuation marks).
Code previously designed for a website (also the length interception function currently used on the homepage)

//$cutlength is the length of the interception (i.e. the number of words)
function cut_str($sourcestr,$cutlength)
{
$returnstr='';
$i=0;
$n=0;
$str_length=strlen($sourcestr);//Number of bytes of string
while (($n{
$temp_str=substr($sourcestr,$i,1);
$ascnum=Ord($temp_str );//Get the ASCII code of the $i-th character in the string
if ($ascnum>=224) //If the ASCII bit is higher than 224,
{
$returnstr=$returnstr.substr ($sourcestr,$i,3); //According to the UTF-8 encoding specification, 3 consecutive characters are counted as a single character
$i=$i+3; //The actual Byte is counted as 3
$n++; //String length is counted as 1
}
elseif ($ascnum>=192) //If the ASCII bit is higher than 192,
{
$returnstr=$returnstr.substr($ sourcestr,$i,2); //According to the UTF-8 encoding specification, 2 consecutive characters are counted as a single character
$i=$i+2; //The actual Byte is counted as 2
$n++ ; //String length is 1
}
elseif ($ascnum>=65 && $ascnum{
$returnstr=$returnstr.substr ($sourcestr,$i,1);
$i=$i+1; //The actual number of Bytes still counts as 1
$n++; //But considering the overall aesthetics, capital letters count as a high digit Characters
}
else //In other cases, including lowercase letters and half-width punctuation marks,
{
$returnstr=$returnstr.substr($sourcestr,$i,1);
$i=$i+1; //The actual Byte count is 1
$n=$n+0.5; //Lowercase letters and half-width punctuation marks are half the width of high-order characters...
}
}
if ($str_length>$cutlength){
$returnstr = $returnstr . "...";//Add an ellipsis at the end when the length exceeds
}
return $ returnstr;
}
Interception utf-8 string function

{
if($len == "") $len=strlen($title);
if($start != 0)
{
$startv = ord(substr($title,$start,1));
if($startv >= 128)
{
if($startv {
for($i=$start-1;$i>0;$i--)
{
$tempv = ord(substr($title,$i,1));
if($tempv >= 192) break;
}
$start = $i;
}
}
}
if(strlen($title)
$alen = 0;
$blen = 0;
$realnum = 0;
for($i=$start;$i
$ctype = 0;
$cstep = 0;
$cur = substr($title,$i,1);
if($cur == "&")
{
if(substr($title,$i,4) == "<")
{
$cstep = 4;
$length += 4;
$i += 3;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,4) == ">")
{
$cstep = 4;
$length += 4;
$i += 3;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,5) == "&")
{
$cstep = 5;
$length += 5;
$i += 4;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,6) == """)
{
$cstep = 6;
$length += 6;
$i += 5;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(preg_match("/(\d+);?/i",substr($title,$i,8),$match))
{
$cstep = strlen($match[0]);
$length += strlen($match[0]);
$i += strlen($match[0])-1;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}
}else{
if(ord($cur)>=252)
{
$cstep = 6;
$length += 6;
$i += 5;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}elseif(ord($cur)>=248){
$cstep = 5;
$length += 5;
$i += 4;
$realnum ++;
if($magic)
{
$ctype = 1;
$blen ++;
}
}elseif(ord($cur)>=240){
$cstep = 4;
$length += 4;
$i += 3;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}elseif(ord($cur)>=224){
$cstep = 3;
$length += 3;
$i += 2;
$realnum ++;
if($magic)
{
$ctype = 1;
$blen ++;
}
}elseif(ord($cur)>=192){
$cstep = 2;
$length += 2;
$i += 1;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}elseif(ord($cur)>=128){
$length += 1;
}else{
$cstep = 1;
$length +=1;
$realnum ++;
if($magic)
{
if(ord($cur) >= 65 && ord($cur) {
$blen++;
}else{
$alen++;
}
}
}
}
if($magic)
{
if(($blen*2+$alen) == ($len*2)) break;
if(($blen*2+$alen) == ($len*2+1))
{
if($ctype == 1)
{
$length -= $cstep;
break;
}else{
break;
}
}
}else{
if($realnum == $len) break;
}
}
unset($cur);
unset($alen);
unset($blen);
unset($realnum);
unset($ctype);
unset($cstep);
return substr($title,$start,$length);
}

TooptimizePHPcodeforreducedmemoryusageandexecutiontime,followthesesteps:1)Usereferencesinsteadofcopyinglargedatastructurestoreducememoryconsumption.2)LeveragePHP'sbuilt-infunctionslikearray_mapforfasterexecution.3)Implementcachingmechanisms,suchasAPC

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor
