fork download
  1. <?php
  2.  
  3. function suffix($number){
  4. $suffix = array('th','st','nd','rd','th','th','th','th','th','th');
  5. if (($number %100) >= 11 && ($number%100) <= 13)
  6. $abbreviation = $number. 'th';
  7. else
  8. $abbreviation = $number. $suffix[$number % 10];
  9.  
  10. return $abbreviation;
  11. }
  12.  
  13. $source = "oasdfyoasdfyoasdfyoasdfy";
  14. $startIndexes =array(0, 6, 12, 18);
  15.  
  16. for ($i=0; $i < count($startIndexes); $i++){
  17. $index= $startIndexes[$i];
  18. $len = ($i< count($startIndexes)-1 ? $startIndexes[$i +1] :
  19. strlen($source)) - ($index);
  20.  
  21. echo sprintf("The %s substring is:[%s]\n",
  22. suffix($i+1),
  23. substr($source, $index, $len) );
  24. }
  25.  
  26. ?>
Success #stdin #stdout 0.01s 20520KB
stdin
Standard input is empty
stdout
The 1st substring is:[oasdfy]
The 2nd substring is:[oasdfy]
The 3rd substring is:[oasdfy]
The 4th substring is:[oasdfy]