skip to main | skip to sidebar

Harish Blog

php Substring Examples

Use substr( ) to select your substrings:

$substring = substr($string,$start,$length);
$username = substr($_REQUEST['username'],0,8);

1.2.2. Discussion

If $start and $length are positive, substr( ) returns $length characters in the string, starting at $start. The first character in the string is at position 0:

print substr('watch out for that tree',6,5);
out f

If you leave out $length, substr( ) returns the string from $start to the end of the original string:

print substr('watch out for that tree',17);
t tree

If $start plus $length goes past the end of the string, substr( ) returns all of the string from $start forward:

print substr('watch out for that tree',20,5);
ree

If $start is negative, substr( ) counts back from the end of the string to determine where your substring starts:

print substr('watch out for that tree',-6);
print substr('watch out for that tree',-17,5);
t tree
out f

If $length is negative, substr( ) counts back from the end of the string to determine where your substring ends:

print substr('watch out for that tree',15,-2);
print substr('watch out for that tree',-4,-1);
hat tr
tre

 

Php Php

No comments:

Post a Comment

Newer Post Older Post Home
Subscribe to: Post Comments (Atom)

Labels

  • Ajax (4)
  • Html (1)
  • jQuery (1)
  • Linux (5)
  • Mysql (13)
  • Netezza (1)
  • Oracle and Php (2)
  • Others (4)
  • Php (15)
  • Shell scripts (1)
  • ಟೆಕ್ news (1)

Blog Archive

  • ►  2011 (1)
    • ►  January (1)
  • ►  2010 (2)
    • ►  October (1)
    • ►  March (1)
  • ►  2009 (11)
    • ►  December (3)
    • ►  October (7)
    • ►  February (1)
  • ▼  2008 (57)
    • ►  October (7)
    • ►  September (10)
    • ►  August (6)
    • ►  July (4)
    • ►  June (4)
    • ►  May (5)
    • ▼  April (11)
      • Some interesting Bugs in php
      • pregquote
      • How does one configure PHP to use Oracle?
      • How does one configure PHP to use Oracle?
      • Mysql Questions..
      • Php Questions...
      • batch file with php
      • How to take the selected tables dump using command...
      • php Substring Examples
      • Php Questions
      • PHP MAGIC CONSTANTS
    • ►  March (2)
    • ►  February (7)
    • ►  January (1)
  • ►  2007 (4)
    • ►  December (3)
    • ►  March (1)
  • ►  2006 (4)
    • ►  November (1)
    • ►  October (3)