如何在 PHP 中使用"…"主体缩短字符串?
phpserver side programmingprogramming更新于 2025/9/6 8:37:17
假设我们的字符串是 −
$sentence="This is my first PHP program";
我们希望输出结果为 −
This is my first PHP ... gram
我们用 “…hellip;” 缩短字符串。为此,使用 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

