PHP 之 字串處理小結

2021-06-03 06:36:29 字數 2753 閱讀 1420

<?php

// 3456

echo substr ( "123456", 2, 4 );

echo "

";$str = "123";

// 不再使用echo $str[1];

echo $str ;

// this hello today

echo "this

hello

today";

// thisisme

echo "this", "is", "me";

echo "

";$str = "php";

$number = 232;

printf ( "%0.3f

", $number );

echo "

";$format = "the %2\$s book contains %1\$d pages. that's

a nice %2\$s full of %1\$d pages.

";printf ( $format, $number, $str );

// 12

// 6

$str = " php2 ";

echo strlen ( $str );

echo "

";echo strlen ( ltrim ( $str ) );

echo "

";// this is a test..

$str = "123 this is a test...";

echo ltrim ( $str, "0..9" );

echo "

";echo rtrim ( $str, "." );

// his is a test

echo trim ( $str, "0..9 a..z." );

echo "

";/*

* php3 ---php3--- ------php3

*/$str = "php3";

echo str_pad ( $str, 10 ); // 預設在右邊

echo "

";echo str_pad ( $str, 10, "-", str_pad_both );

echo "

";echo str_pad ( $str, 10, "-", str_pad_left );

echo "

";echo strtoupper ( $str );

echo "

";/*

* 1326702908

1128290400

1326720908

1327307708

1327592113

1327273200

1326582000

*/echo (strtotime ( "now" ));

echo "

";echo (strtotime ( "3 october 2005" ));

echo "

";echo (strtotime ( "+5 hours" ));

echo "

";echo (strtotime ( "+1 week" ));

echo "

";echo (strtotime ( "+1 week 3 days 7 hours 5 seconds" ));

echo "

";echo (strtotime ( "next monday" ));

echo "

";echo (strtotime ( "last sunday" ));

echo "

";//php3

echo ucfirst($str);

echo "

";//this is a test

echo ucwords("this is a test");

//echo ucfirst(strtolower($str));

echo "

";/*

* one line.

next line

*/echo nl2br("one line.\n next line");

echo "

";$str = "webserver:&'linux'&'apache'";

//webserver:&'linux'&'apache',黑體

echo $str."

";//不會有標籤效果

//webserver:&'linux'&'apache'

//原始碼:webserver:&'linux'&'apache'

echo htmlspecialchars($str,ent_compat,"utf-8");//轉換hmtl和雙引號

echo "

";$str = "乙個 'quote' 是bold";

//ò»¸ö 'quote' êçbold//htmlentities() 可以將所有非ascii碼字元轉化為 對應的實體**

//可以轉義更多的html字元

echo htmlentities($str);

echo "

";//$str = "乙個 'quote' 是bold";

//沒有出中文

echo htmlentities($str,ent_quotes,'utf-8');

echo phpinfo();

?>

php 字串處理

一 字串格式化 1.trim 可以出去字串開始位置和結束位置的空格,並返回結果字串 ltrim 除去左邊空格 rtrim 除去右邊空格 2.nl2br 用代替字串中的換行符 3.printf 將乙個格式化的字串輸出到瀏覽器中 sprintf 返回乙個格式化的字串 例 printf total amo...

PHP字串處理

雙引號中的內容可以被解釋與替換,單引號中的字元當做普通字元處理 使用 或 一大波函式將來襲。strlen 計算字串長度 substr string str,int start,int length 從start出擷取長度為length的字串 strcmp str1 str2 區分大小寫 strcas...

php字串處理

字串擷取 substr 對字串進行指定數量的擷取 strchr strstr別名 查詢乙個字串在另乙個字串中第一次出現,返回字串到結尾 stristr strrchr 查詢乙個這符串在另乙個字串中最後出現的位置 strpos 從頭查詢乙個字串在另乙個字串中第一次出現的位置,區分大小寫 stripos...