개발! 딱 깔끔하고 센스있게!

짧은 텍스트, PHP 본문

개발 스크랩 메모/PHP

짧은 텍스트, PHP

렉사이 2020. 12. 25. 23:52

이 기능이 있습니다:

function shorter($text, $chars_limit) { if (strlen($text) > $chars_limit) return substr($text, 0, strrpos(substr($text, 0, $chars_limit), " ")).'...'; else return $text; } 

만약 내가 echo shorter ($input, 11) 을 사용하면 일을 정상적으로 할 수 있지만, 만약 입력 중 일부 공백을 입력하면 비슷해 보이기:

wwwwwwwwwww

이 함수는 이 변경:

... (3 dots).

나는 그것을 이렇게 만들고 싶지 않다:

www ...

너는 이 대본을 어떻게 재건할 줄 아니?미리 감사합니다.

대답

나는 네가 단지 입력을 받아들이고 싶다고 가정했다.

X 보다 길다면, X에서 그것을 잘라서 '...

// Start function function shorter($text, $chars_limit) { // Check if length is larger than the character limit if (strlen($text) > $chars_limit) { // If so, cut the string at the character limit $new_text = substr($text, 0, $chars_limit); // Trim off white space $new_text = trim($new_text); // Add at end of text ... return $new_text . "..."; } // If not just return the text as is else { return $text; } } 

나는 이것을 테스트하지 않았지만, 그것은 반드시 유효해야 한다.

)

Comments