用一个 PHP 中以 “…” 主体的字符串缩短字符串?
假设我们的字符串如下 -
$sentence="This is my first PHP program";
我们想要以下输出 -
This is my first PHP ... gram
我们使用 “…” 缩短字符串。为此,请使用 substring() 的概念,并通过检查单词数量来设置条件 -
示例
<!DOCTYPE html>
<html>
<body>
<?php
$sentence="This is my first PHP program";
if (strlen($sentence) >= 13) {
echo substr($sentence, 0, 20). " ... " . substr($sentence, -4);
} else {
echo $sentence;
}
?>
</body>
</html>输出
This is my first PHP ... gram
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP