php 常用字串函數總結_PHP教程

來源:互聯網
上載者:User
1.格式化輸出

chop 是rtrim()的別名;

ltrim()

trim()

nl2br()將\n轉換成


print,echo,printf(),sprintf():

echo()不是函數,print()是函數,有傳回值,boolen,false,true;

printf()格式化輸出

--函數,把文字格式化以後輸出,直接調用系統調用進行IO的,他是非緩衝的。如:
$name="hunte";
$age=25;
printf("my name is %s, age %d", $name, $age);

sprintf()格式化字串,然後賦給一個變數,但是不輸出,類似於c了

echo nl2br("foo isn't\n bar");
echo "foo isn't\n bar";
?>

--跟printf相似,但不列印,而是返回格式化後的文字,其他的與printf一樣。如:
char sql[256];
sprintf(sql,"select * from table where no = '%s'",bankno);
它的功能只是把""裡面的語句賦給了變數sql。

strtolower

strtoupper

ucwords

ucfirst

2.字串的串連和分割

(1)array explode(string input ,string separator , int limit)

使用一個字串分割另一個字串

// 樣本 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 參數樣本

$str = 'one|two|three|four';

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

// 負數的 limit
print_r(explode('|', $str, -1));
?>



string strtok( string input ,string separator)

$string = "This is\tan example\nstring";
/* Use tab and newline as tokenizing characters as well */
$tok = strtok($string, " \n\t");

//拿著空格,\n,\t作為令牌分割字串

while ($tok !== false) {
echo "Word=$tok
";
$tok = strtok(" \n\t");
}
?>

結果:

Word=This
Word=is
Word=an
Word=example
Word=string

(2.)字串的截取

$test="Your customer service is excellent";
echo substr($test,1);////////our customer service is excellent
echo "
";
echo substr($test,-9);//////從末尾起長度是9excellent
echo "
";
echo substr($test,0,4);////從0位置開始長度是4Your
echo "
";
echo substr($test,5,-13);/從第四個開始到倒數第13個字元customer service
echo "
";

$test="Your customer service is excellent";
echo substr($test,1);
echo "
";
echo substr($test,-11);
echo "
";
echo substr($test,0,6);
echo "
";
echo substr($test,5,-13);
echo "
";

our customer service is excellent
s excellent
Your c
customer service

(3)join()字串的連結

3.字串的尋找


(1)string strstr ( string haystack, string needle ) 別名:strchr,stristr和strstr類似不同在於不區分大小寫

strrchr()相反的,尋找的是最後一次出現的字串

第一次出現起的字串
$email = 'user@example.com';
$domain = strstr($email, '@');
echo $domain; // prints @example.com
?>
$email = 'user@example.com';
$domain =strstr($email,'e');
$domain2 =strrchr($email,'e');//最後一次出現起的字串
echo $domain;
echo "
";
echo $domain2;

er@example.com
e.com

(2)尋找位置

int strpos(string str,string needle,[int offset]) 沒有找到返回的是false

返回從offset開始在str中尋找needle的位置

$eg:$t-'hello world';

echo strpos($t,'o',5);

//7 從第o開始,尋找o這個變數的位置,結果為7

int strrpos()

5.替換

str_replace("%body%","blank","





6 。大寫小問題


Strpos
尋找字串中第一次出現的字串的位置

Strrpos
尋找字串中某字元,繼第一次之後的最先出現的位置。

strpos(stripos無大小寫)
strrpos(strripos無大小寫)






strstr
stristr(無大小寫)

str_replace
str_ireplace(無大小寫)

http://www.bkjia.com/PHPjc/318672.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/318672.htmlTechArticle1.格式化輸出 chop是rtrim()的別名; ltrim() trim() nl2br()將\n轉換成br print,echo,printf(),sprintf(): echo()不是函數,print()是函數,有傳回值,boolen,...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.