Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* Simple function to sample a string, at a space. Default Length is 80 */
- function sample($string, $length=80){
- $string = substr($string, 0, $length);
- return substr($string, 0, strrpos($string, ' '));
- }
- $block = "This is some string. I think it needs to continue for a while, but I'm not entirely sure where to cut it. So I think I will simply keep adding to it, while not counting the neccessary characters for the length. Knowing this would allow me to know when to cut the string.";
- $sample = sample($block);
- echo $sample . '<br>';
- echo 'Strlen: ' . strlen($sample) . '<br>';
- /* Ouputs:
- This is some string. I think it needs to continue for a while, but I'm not
- Strlen: 74
- */
- $sample2 = sample($block, 120);
- echo $sample2 . '<br>';
- echo 'Strlen: ' . strlen($sample2) . '<br>';
- /* Outputs:
- This is some string. I think it needs to continue for a while, but I'm not entirely sure where to cut it. So I think I
- Strlen: 118
- */
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement