用一個 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