Last active
November 11, 2019 08:03
-
-
Save bwonur/af9566ed768b3a3751a859ed4a88ca61 to your computer and use it in GitHub Desktop.
Truncate String ( Yazı kısıtlama )
This file contains hidden or 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 | |
| function trunc($phrase, $max_words) { | |
| $phrase_array = explode(' ',$phrase); | |
| if(count($phrase_array) > $max_words && $max_words > 0) | |
| $phrase = implode(' ',array_slice($phrase_array, 0, $max_words)).'...'; | |
| return $phrase; | |
| } | |
| ?> | |
| OR | |
| <?php | |
| function limit_words($words, $limit, $append = ' …') { | |
| // Add 1 to the specified limit becuase arrays start at 0 | |
| $limit = $limit+1; | |
| // Store each individual word as an array element | |
| // Up to the limit | |
| $words = explode(' ', $words, $limit); | |
| // Shorten the array by 1 because that final element will be the sum of all the words after the limit | |
| array_pop($words); | |
| // Implode the array for output, and append an ellipse | |
| $words = implode(' ', $words) . $append; | |
| // Return the result | |
| return $words; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment