note 45227 added to function.substr
| From: | Biomech at osu1 dot php dot net | Date: | Mon, 30 Aug 2004 14:05:08 +0000 |
| Subject: | note 45227 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-75679@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45227
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45227&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45227&report=yes
Search -- http://master.php.net/manage/user-notes.php