note 45227 deleted from function.substr by aidan

From: Date: Tue, 18 Jan 2005 15:41:16 +0000
Subject: note 45227 deleted from function.substr by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-83563@lists.php.net to get a copy of this message
Note Submitter: Biomech ---- Another useful implimentation of substr() is with moving words ie: "The","Mr,"A", in particular, titles that you want displayed in the format "title,the". You'll have to excuse any bad syntax Im still working my way through the manual. <?php // DEFINE ARRAY (or from database) $foo = array("The Matrix","The Mothman Prophecies","Wargames","Donnie Darko","The Hole"); $elements = count($foo); // COUNT ELEMENTS $the = ",The"; // DEFINE SUFFIX $i = 0; // DEFINE INCREMENT(helps when E_ALL is on) // LOOP THROUGH TITLES IN ARRAY while($i<=$elements) { $title = $foo[$i]; // GET TITLE // IF STARTS "The "... if(substr($title,0,4) == "The ") { $title = substr($title,4); // PULL OFF "The" $title .= $the; // PUT IT ON THE END } echo $title ."<br>"; // PRINT TITLE $i++; } ?> Note the space in the if(). This is required so that you dont ammend words that start with "the", for example: Thelma and Louise, Theatre of Death etc

« previous php.notes (#83563) next »