String Functions : Smart substr()

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
mudkicker
Forum Contributor
Posts: 479
Joined: Wed Jul 09, 2003 6:11 pm
Location: Istanbul, TR
Contact:

String Functions : Smart substr()

Post by mudkicker »

Code: Select all

<?php
function substr_smart($string, $int, $long)
&#123;
    // www.mudkicker.com
    $string = substr($string, $int, $long);
    $array = explode(' ', $string);
    array_pop($array);
    $string = implode(' ', $array);
    $string = trim($string);
    return $string;
&#125;
?>
This function don't splits words if the 100. character is in the middle of a word..
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

preg_match('#^\s*(.&#123;26,&#125;?)\s+.*$#', $text, $match);
viewtopic.php?t=25351
from useful posts :P
Post Reply