


PHP custom function to intercept the length of Chinese characters_PHP tutorial
function msubstr($str,$start,$len) {
$strlen=$start+ $len;
for($i=0;$i<$strlen;$i++) {
if(ord(substr($str,$i,1))>0xa0) {
$ tmpstr.=substr($str,$i,2);
$i++;
} else
$tmpstr.=substr($str,$i,1);
}
return $tmpstr;
}
< ;?PHP
$str="This character is so long,^_^";
$Short_Str=showShort($str,4);//Intercept the first 4 Chinese characters, the result is: this character.. .
Echo "$Short_Str";
Function csubstr($str,$start,$len)
{
$strlen=strlen($str);
$clen=0;
for($i=0;$i<$strlen;$i++,$clen++)
{
if ($clen>=$start+$len)
break;
if(ord (substr($str,$i,1))>0xa0)
{
if ($clen>=$start)
$tmpstr.=substr($str,$i,2);
$i++;
}
else
{
if ($clen>=$start)
$tmpstr.=substr($str,$i,1);
}
}
return $tmpstr;
}
Function showShort($str,$len)
{
$tempstr = csubstr($str,0,$ len);
if ($str<>$tempstr)
$tempstr .= "..."; //Whatever you want to end with, just modify it here.
return $tempstr ;
}
How about the nagging method, concise?
$len = 19;
$text = "How to display only the first few words of a long news title and replace it with...? ";
echo strlen($text)<=$len ? $text : (substr($text,0,$len).chr(0)."....");

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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



A function is a set of reusable code blocks that perform a specific task (have a specific functionality). In addition to using built-in functions, we can also create our own functions (custom functions) and then call this function where needed. This not only avoids writing repeated code, but also facilitates the later maintenance of the code.

How to use Python to write custom functions in MySQL MySQL is an open source relational database management system that is often used to store and manage large amounts of data. As a powerful programming language, Python can be seamlessly integrated with MySQL. In MySQL, we often need to use custom functions to complete some specific calculations or data processing operations. This article will introduce how to use Python to write custom functions and integrate them into MySQL. For writing custom functions,

How to write custom stored procedures and functions in MySQL using PHP In the MySQL database, stored procedures and functions are powerful tools that allow us to create custom logic and functions in the database. They can be used to perform complex calculations, data processing and business logic. This article will introduce how to write custom stored procedures and functions using PHP, with specific code examples. Connecting to the MySQL database First, we need to connect to the MySQL database using the MySQL extension for PHP. can use

In PHP, a function is a set of reusable blocks of code that are identified by a name. PHP supports a large number of ready-made functions, such as array_push, explode, etc., but sometimes you need to write your own functions to implement specific functions or improve code reusability. In this article, I will introduce how to customize functions in PHP, including function declaration, calling and using function parameters. Declaration of functions To declare a function in PHP, you need to use the keyword function. The basic syntax of the function is as follows:

PHP custom functions allow encapsulating code blocks, simplifying code and improving maintainability. Syntax: functionfunction_name(argument1,argument2,...){//code block}. Create function: functioncalculate_area($length,$width){return$length*$width;}. Call the function: $area=calculate_area(5,10);. Practical case: Use a custom function to calculate the total price of the items in the shopping cart, simplifying the code and improving readability.

Basic knowledge of functions: Master the basic syntax specifications and calling methods of custom functions, and master the usage and calling rules of various parameters of functions. 1. Python function (Function) is an organized, reusable code segment used to implement a single or related function. Functions can improve application modularity and code reuse. We have already touched on many of the built-in functions provided by Python, such as print(). But you can also create your own functions, which are called user-defined functions. 2. Basic rules for customizing a function. You can define a function with the functions you want. The following are simple rules: the function code block starts with the def keyword, followed by the function identifier name and parentheses.

Function parameters are the bridge between the inside of the function and the outside of the function. The following article will take you through the parameters in JavaScript functions. I hope it will be helpful to you!

Golang is a very popular programming language with a very powerful function library. In Golang, functions are considered first-class citizens, which means that Golang functions can be passed, copied, and overloaded like variables. In addition, Golang also provides two types of built-in functions and custom functions. In this article, we will explore the pros and cons of built-in functions and custom functions in Golang to help readers understand when to choose which type of function. First, let's look at the built-in functions. built-in letter
