Created
September 11, 2017 11:48
-
-
Save ArtemioVegas/2d047b907ab9f701a7e49df44fbecab4 to your computer and use it in GitHub Desktop.
обрезать строку и добавить в конец '...', если та больше заданной длинны.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Нужно: обрезать строку и добавить в конец '...', если та больше заданной длинны. | |
* | |
* На входе два параметра | |
* $string - строка | |
* $lenght - длинна | |
* | |
* Пример: | |
* $tring = 'John Doe' | |
* $lenght = 4 | |
* | |
* То, на выходе должно быть 'John...' | |
* | |
*/ | |
function testString1(string $str, int $lenght){ | |
if($lenght <= 0 || $lenght >= strlen($str)){ | |
return $str; | |
}else{ | |
$result = substr($str, 0, $lenght); | |
return $result ? $result.'...':false; | |
} | |
} | |
var_dump(testString1('Johnyy',10)); | |
var_dump(testString1('Joh',2)); | |
var_dump(testString1('Joh',0)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment