The solution to the problem that the php string is too long: first intercept the length equal to 0 or greater than or equal to the length of the string, then return the string itself; then if the intercepted length is a negative number, then the intercepted length is equal to the length of the string Subtract the interception length; finally, if the absolute value of the interception length is greater than the length of the string itself, the interception length is the length of the string itself.
[Related learning recommendations: php programming (video)]
Solution to php string that is too long:
Use the method of judging the length of the string
if (! function_exists('mbSubStr')){ function mbSubStr($str, $length = 0, $append = true) { $str = trim($str); $strlength = strlen($str); if ($length == 0 || $length >= $strlength) { return $str; //截取长度等于0或大于等于本字符串的长度,返回字符串本身 }elseif ($length < 0){ //如果截取长度为负数 $length = $strlength + $length;//那么截取长度就等于字符串长度减去截取长度 if ($length < 0) { $length = $strlength;//如果截取长度的绝对值大于字符串本身长度,则截取长度取字符串本身的长度 } } if (function_exists('mb_substr')){ $newstr = mb_substr($str, 0, $length, 'utf-8'); }elseif (function_exists('iconv_substr')){ $newstr = iconv_substr($str, 0, $length, 'utf-8'); }else{ //$newstr = trim_right(substr($str, 0, $length)); $newstr = substr($str, 0, $length); } if ($append && $str != $newstr){ $newstr .= '...'; } return $newstr; } }
If you want to know more about programming learning, please pay attention to the php training column!
The above is the detailed content of What to do if the php string is too long. For more information, please follow other related articles on the PHP Chinese website!

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

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver CS6
Visual web development tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

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

Atom editor mac version download
The most popular open source editor
