PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

 

Code Snippets

  

PHP Source Code


You're Browsing As A Guest! Register Now...
Become a PHP Expert!

Join 358,767 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 3,807 people online right now.Registration is fast and FREE... Join Now!




PHP left() and right()

PHPs version of the ASP functions left() and right()

Submitted By: pemcconnell
Actions:
Rating:
Views: 18,612

Language: PHP

Last Modified: October 28, 2009
Instructions: Add functions to top of code, then call them using the following syntax:


Snippet


  1. <?php
  2.  
  3. function right($value, $count){
  4.     return substr($value, ($count*-1));
  5. }
  6.  
  7. function left($string, $count){
  8.     return substr($string, 0, $count);
  9. }
  10.  
  11. ?>

Copy & Paste


Comments

Master Jake 2009-10-25 13:23:55

Actually, you can just use substr($string, -1) to return to rightmost character in a string. Replace -1 with how many characters from the right you want to return, of course.

pemcconnell 2009-10-26 03:20:26

Thanks for the heads up Master Jake, I was unaware it supported negative values. I've updated the snippet to support your recommendations and in turn have a faster execution time.


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month