★ wanayoo — archive 1999 http://www.php.net/manual/function.substr.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Strings
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Strings
* AddCSlashes
* AddSlashes
* bin2hex
* Chop
* Chr
* chunk_split
* convert_cyr_string
* count_chars
* crc32
* crypt
* echo
* explode
* flush
* get_html_translation_table
* get_meta_tags
* hebrev
* hebrevc
* htmlentities
* htmlspecialchars
* implode
* join
* levenshtein
* ltrim
* md5
* Metaphone
* nl2br
* ob_start
* ob_get_contents
* ob_end_flush
* ob_end_clean
* ob_implicit_flush
* Ord
* parse_str
* print
* printf
* quoted_printable_decode
* quotemeta
* rtrim
* sscanf
* setlocale
* similar_text
* soundex
* sprintf
* strcasecmp
* strchr
* strcmp
* strcspn
* strip_tags
* stripcslashes
* stripslashes
* stristr
* strlen
* str_pad
* strpos
* strrchr
* str_repeat
* strrev
* strrpos
* strspn
* strstr
* strtok
* strtolower
* strtoupper
* str_replace
* strtr
* substr
* substr_count
* substr_replace
* trim
* ucfirst
* ucwords
Manual: substr
View the source code for this pageSearch the site



Previous page
 strtr
 Updated
Sat, 12 Aug 2000
substr_count 
Next page


substr

(PHP3 , PHP4 )

substr -- Return part of a string

Description

string substr (string string, int start [, int length])

Substr returns the portion of string specified by the start and length parameters.

If start is positive, the returned string will start at the start'th character of string.

Examples:


$rest = substr ("abcdef", 1);    // returns "bcdef"
$rest = substr ("abcdef", 1, 3); // returns "bcd"
      

If start is negative, the returned string will start at the start'th character from the end of string.

Examples:


$rest = substr ("abcdef", -1);    // returns "f"
$rest = substr ("abcdef", -2);    // returns "ef"
$rest = substr ("abcdef", -3, 1); // returns "d"
      

If length is given and is positive, the string returned will end length characters from start. If this would result in a string with negative length (because the start is past the end of the string), then the returned string will contain the single character at start.

If length is given and is negative, the string returned will end length characters from the end of string. If this would result in a string with negative length, then the returned string will contain the single character at start.

Examples:


$rest = substr ("abcdef", 1, -1); // returns "bcde"
      

See also strrchr() and ereg().


User Contributed Notes: substr


lynch@cognitivearts.com
26-Jan-1999 08:45
Shouldn't that text before the last example say ... before the START of string?... I mean, the example *seems* to be counting 1 characters from the start of string.
It might also be worth saying what happens with:
substr("abcdef", -7, 3);
I don't think anything here really addresses that... although I'd guess it might be "a". Or it might be smart and give me "ab". Hmmmm.



mtsinc@leading.net
19-Jun-1999 06:45
"If start is positive, the returned string will start at the start'th character of string."


Start is the offset from the first character, and start = 2 means begin at the third character.

Unless otherwise explicitly stated, anywhere else in the string docs that has a "character number" I'd take it that it acts similarly.



yzhang@sfu.ca
20-Jul-1999 12:18
If you want to do pad a string from the left (eg. from "8" to "0008"), you can use this:

$num = substring("0000".$num, -4, 4)



brian@vinylbaby.com
22-Aug-1999 02:37
If you want to start with the first character in 'string' you need to use '0'.


rattlesnake@UniNova.zzn.com
20-Feb-2000 04:33
The documentation regarding the length argument is incomplete. It should be stated that leaving out the length will return the substring from the start position to the end of the string. (This may seem "obvious" to some, but it should be clear from the "discussion" regarding the first element of an array that details like this shouldn't be taken for granted.)



tim@sod.co.uk
20-Apr-2000 05:22
If you want to make it start at 1 you can add this simple code to any script:


$one=0;

substr("Hello",$one,5);



jpalonus@halcyon.com
08-May-2000 04:07
Getting back to the problem, here's what the manual says:


"If start is positive, the returned string will start at the start'th character of string."


But as the first two examples show, if start is 1, the returned string starts at the *2nd* character, *not* the 1st.


The description is wrong. The example is correct.



rdany@nbnet.nb.ca
29-Jul-2000 03:34
The correction...

If start is positive, the returned string will start at the start'th character of string.

Should be:

If start is positive, the returned string will start at the start'th position in string.

(That considering people know array is reference from 0. If someone doesn't know about it. They'll figure it out. Eventually.)



 About Notes


Previous page
 strtr
 Updated
Sat, 12 Aug 2000
substr_count 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.