note 13421 added to ref.strings.php

From: Date: Fri, 15 Jun 2001 19:45:09 +0000
Subject: note 13421 added to ref.strings.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-14283@lists.php.net to get a copy of this message
I have written an *evil* little function that will cut a string on a character given. Features: 1) Minimum length. 2) If a string doesn't contain the character, it cuts your string to minimum length. 3) If the next character beyond the minimumlength equals the given character, it will cut the string to minimulength as well. Code: <?php function cuttext($tring,$cuton) { /* Define the character to cut on */ /* One could make an array with all characters to cut on */ /* But I wouldn't know how to :) */ $space=" "; if (!strstr($tring,$space)) { /* No space is found in the whole string: cut at 20th character */ $tring=substr($tring,0,$cuton); } if (substr($tring,$cuton,1)==$space) { /* 21st character is a space: cut at 20th character */ $tring=substr($tring,0,$cuton); } else { /* 21st Character is NOT a space: return to last space and cut there */ while ($teller <= $cuton) { if (substr($tring,$cuton-$teller,1)==$space) { $tring=substr($tring,0,$cuton-$teller); break; } $teller++; } } return $tring; } echo(cuttext("waf groink muha zz gruu wrrreea iii gargghhh >:)>:)",20)); ?> http://www.php.net/manual/en/ref.strings.php

« previous php.notes (#14283) next »