php常用文字列関数のまとめ

4142 ワード

1.フォーマット出力
チョプはテリムの別名です。
ltrim()
trim()
nl 2 br()を
に変換します。
print、echo、printf()、sprintf():
echo()は関数ではなく、print()は関数で、戻り値、bootlen、false、trueがあります。
printf()フォーマット出力
--関数は、テキストをフォーマットして出力し、直接システムを呼び出してIOを行うもので、バッファではありません。例えば:
$name="hunt";
$age=25;
printf("my name is%s,age%d",name,$age);
sprintf()は文字列をフォーマットして、変数を与えますが、出力しません。cに似ています。

<?php 
echo nl2br("foo isn't
bar"); echo "foo isn't
bar"; ?>
--printfと似ていますが、印刷ではなく、書式設定された文字を返します。他のものはprintfと同じです。例えば:
char sql[256]
sprintf(sql,"select*from table where no='%s',bankno);
その機能は「」の中の語句を変数sqlに与えただけです。
strower
sttouper
タッチステーション
ucfirst
2.文字列の接続と分割
(1)array explode(string input、string separator、int limit)
他の文字列を分割する文字列を使用します。

<?php 
//    1 
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6"; 
$pieces = explode(" ", $pizza); 
echo $pieces[0]; // piece1 
echo $pieces[1]; // piece2 

//    2 
$data = "foo:*:1023:1000::/home/foo:/bin/sh"; 
list($user, $pass, $uid, $gid, $gecos, $home, $shell) = explode(":", $data); 
echo $user; // foo 
echo $pass; // * 
?> 
例2.limitパラメータ例

<?php 
$str = 'one|two|three|four'; 

//     limit 
print_r(explode('|', $str, 2)); 

//     limit 
print_r(explode('|', $str, -1)); 
?> 
string stritok(string input、string separator)

<?php 
$string = "This is\tan example
string"; /* Use tab and newline as tokenizing characters as well */ $tok = strtok($string, "
\t"); // ,
,\t while ($tok !== false) { echo "Word=$tok<br />"; $tok = strtok("
\t"); } ?>
結果:
Word=This
Word=is
ワード=an
Word=example
ワード=string
(2.)文字列の切り取り

$test="Your customer service is excellent"; 
echo substr($test,1);////////our customer service is excellent 
echo "<br>"; 
echo substr($test,-9);//////       9excellent 
echo "<br>"; 
echo substr($test,0,4);//// 0       4Your 
echo "<br>"; 
echo substr($test,5,-13);/          13   customer service 
echo "<br>"; 

$test="Your customer service is excellent"; 
echo substr($test,1); 
echo "<br>"; 
echo substr($test,-11); 
echo "<br>"; 
echo substr($test,0,6); 
echo "<br>"; 
echo substr($test,5,-13); 
echo "<br>"; 
our customer service is excelllent
sエクセル
Your c
customer service
(3)ジョイン()文字列のリンク
3.文字列の検索
(1)string str(string haystack、string needle)別名:strer、stristrとstrが似ているのは、大文字と小文字を区別しないことです。
strrchr()は逆で、検索は最後の文字列です。
最初に発生した文字列

<?php 
$email = '[email protected]'; 
$domain = strstr($email, '@'); 
echo $domain; // prints @example.com 
?> 
$email = '[email protected]'; 
$domain =strstr($email,'e'); 
$domain2 =strrchr($email,'e');//            
echo $domain; 
echo "<br>"; 
echo $domain2; 
[email protected]
e.com
(2)検索位置
int string str,string needle,[int offset]が返されていないのはfalseです。
offsetからstrでneedleを検索する位置を返します。
$eg:$t-'hello world'
echo stropos($t、'o'、5)
//7 oから、oという変数の位置を検索します。結果は7です。
int strpos()
5.置換
strreplace(「%body%」、「blank」、「6です。大文字小文字の問題
Strpos
文字列の最初の文字列の位置を検索します。
Strrpos
文字列の中のある文字を検索します。最初の文字に続く最初の場所です。
stops(striposは大文字と小文字がありません)
strpos(striposは大文字と小文字がありません)
str.str
stristr(大文字なし)
strreplace
strireplace(大文字なし)