다음이 우리의 문자열이라고 가정해 봅시다 -
$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