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

PHP Home Page

Manual Table of Contents
Up to 文字列
Quick Reference
English version of this pageGerman version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
文字列
* 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
Mon, 14 Aug 2000
substr_count 
Next page


substr

(PHP3 , PHP4 )

substr -- 文字列の一部分を返す

説明

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

substr() は文字列 string の、start で指定された 位置から length バイト分の文字列を 返します。

start が正の場合、返される文字列は、 stringstart 番目から始まる文字列となります。 例:


$rest = substr("abcdef", 1); // "bcdef" を返します
$rest = substr("abcdef", 1, 3); // "bcd" を返します
      

start が負の場合、返される文字列は、 string の 後ろから数えて start番目から始まる文字列となります。 例:


$rest = substr("abcdef", -1); //  "f" を返します
$rest = substr("abcdef", -2); // "ef" を返します
$rest = substr("abcdef", -3, 1); // "d" を返します
      

length が指定され、かつ正である 場合、返される文字列は start から 数えて length 文字数分となります。 負の length が指定された場合、(start は文字列の終端を過ぎているので) 返される文字列は start の位置にある 1文字のみとなります。

length が指定され、かつ負である 場合、返される文字列は start の後ろ から数えて length 文字までとなります。 この指定により切り出す文字列の長さが負となる場合、 startの位置にある1文字のみが 文字列として返されます。

例:


$rest = substr("abcdef", 1, -1); // "bcde" を返Wます
      

strrchr() および 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
Mon, 14 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.