Home > Backend Development > PHP Tutorial > PHP limits the number of words displayed in article content

PHP limits the number of words displayed in article content

WBOY
Release: 2016-07-29 09:14:53
Original
2905 people have browsed it

php limits the number of words displayed in article content

http://www.3qphp.com/index/show/index/184.html

<?php
 
function cutstr($str,$cutleng)
{
$str = $str; //要截取的字符串
$cutleng =
$cutleng; //要截取的长度
$strleng =
strlen($str); //字符串长度
if($cutleng>$strleng)return
$str;//字符串长度小于规定字数时,返回字符串本身
$notchinanum = 0;//初始不是汉字的字符数
for($i=0;$i<$cutleng;$i++)
{
if(ord(substr($str,$i,1))<=128)
{
$notchinanum++;
}
}
if(($cutleng%2==1)&&($notchinanum%2==0))//如果要截取奇数个字符,所要截取长度范围内的字符必须含奇数个非汉字,否则截取的长度加一
{
$cutleng++;
}
if(($cutleng%2==0)&&($notchinanum%2==1))//如果要截取偶数个字符,所要截取长度范围内的字符必须含偶数个非汉字,否则截取的长度加一
{
$cutleng++;
}
return substr($str,0,$cutleng);
}
?>
Copy after login

2. When needed, call this function --- cutstr
For example:

Click to view the full text

The above introduces PHP's limit on the number of words displayed in article content, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template