login  Naam:   Wachtwoord: 
Registreer je!
 Scripts:

Scripts > PHP > Reguliere expressies > Afkappen string op . , of spatie

Afkappen string op . , of spatie

Auteur: BramBo - 07 januari 2008 - 22:12 - Gekeurd door: Stijn - Hits: 4177 - Aantal punten: (0 stemmen)





aanroep:

  1. $string = substrTxt($limit, $string)


$limit is het maximale aantal tekens waarna hij gaat aftellen.

Edit:
Regexp is eenvoudig aan te passen om bijvoorbeeld alleen komma's en punten te matchen; Verwijder in dit geval de \s in de gehele regel, zowel in group 2( ([\.,\s]+?) ) als in de negative match( [^\.,\s] ) na group 2.

Code:
  1. function substrTxt($limit, $str) {
  2. return (strLen($str)>$limit) ? preg_replace('/(.+?)([\.,\s]+?)[^\.,\s]*?$/si', '$1$2', substr($str, 0, $limit)) : $str;
  3. }

Download code! Download code (.txt)

 Stemmen
Niet ingelogd.

 Reacties
Post een reactie
Lees de reacties (4)
© 2002-2024 Sitemasters.be - Regels - Laadtijd: 0.032s