PHP 內建支援幾種資料型別。除此之外,PHP 還支援許多在處理某些資料時使用的函數。 PHP 字串函數是一些用於操作字串資料的函數。所有這些功能都是預先定義的。需要安裝任何插件。讓我們來看看一些 PHP 字串函數。
廣告 該類別中的熱門課程 PHP 開發人員 - 專業化 | 8 門課程系列 | 3次模擬測驗開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
下面是一些字串函數,並使用以下語法舉例說明。
<?php echo func( "<data>" ); ?>
字串函數使用起來很簡單。這裡我們將透過範例來討論如何在 PHP 程式設計中使用字串函數。
這將傳回一個在特定字元前面帶有反斜線的字串
例如:
echo addcslashes ("Hello World!","W");
輸出:
你好世界
這將傳回預定義字元前面帶有反斜線的字串
例如:
echo addcslashes('Hello "World" you');
輸出:
你好「世界」。
將二進位資料轉換為十六進位資料
例如:
echo bin2hex ("Hello");
輸出:
48656c6c6f
從右端刪除空格或任何預定義字元(如果指定)
例如:
echo chop ("WelcomeBack" , "Back");
輸出:
歡迎
此 PHP 字串函數傳回指定 ASCII 值的字元。
例如:
echo char(52);
輸出:
4
用於將字串分割成更小的部分
例如:
echo chunk_split ($str, 2,", ");
輸出:
我們,lc,om,e,
這將解碼 uuencoded 字串
例如:
echo convert_uudecode ("+22!L;W9E( %!( 4\"$`\n` ");
輸出:
我喜歡 PHP!
convert_uuencode() 與convert_uudecode()相反
此 PHP 字串函數輸出字串中字元數的資料。
例如:
echo count_chars ("Hello", 3);
輸出:
你好
註: 整數值是模式,用來指定所需輸出的型別計算字串的 32 位元循環冗餘校驗和(數學函數)。
例如:
crc32 ("Hello World!");
輸出:
472456355
這將陣列元素與指定的字串連接
例如:
$array = array ('lastname', 'email', 'phone'); echo implode(",", $array);
輸出:
姓氏、電子郵件、電話
注意: join() 也做同樣的事情。它是 implode() 的別名。這會將一些預先定義的字元轉換為 HTML 實體,即它顯示來源。
例如:
$str = "I am <b>Bold</b>"; echo $str; => I am <strong>Bold</strong> echo htmlspecialchars($str);
輸出:
我大膽
此 PHP 字串函數刪除字串左側的空格或預定義字元。
例如:
echo ltrim ("Just a sample", "Just");
輸出:
樣品
注意: rtrim() 從右側執行類似的工作這將數字格式化為千位分組
例如:
echo number_format (1000000);
輸出:
1,000,000
這只是輸出字串並且比 echo 慢
此外,列印不應該與 () 一起使用
例如:
print "Hello";
輸出:
你好
這會計算字串的 md5 雜湊值
例如:
echo md5 ("Hello");
輸出:
8b1a9953c4611296a827abf8c47804d7
這會將字串分割成更小的字串
例如:
$string = "This is to break a string"; $token = strtok ($string, " "); echo($token); => This To get all words of string, while ($token !== false){ echo "$token<br>"; $token = strtok(" "); }
輸出:
這個
是
到
休息
字串
This converts a string to uppercase characters
E.g.:
echo strupper ("Beautiful Day");
Output:
BEAUTIFUL DAY
Note: strlower() converts strings to all lowercase characters.This returns part of the string starting with the index specified
E.g.:
echo subst ("A Hot Day", 3);
Output:
ot Day
This PHP string function replaces a part of the string with the string specified.
E.g.:
echo substr_replace ("Hot", "Day",0);
Output:
Day
This wraps a string to a number of characters
E.g.:
echo wordwrap ("Hello World", 5, "\n");
Output:
Hello
World
This is used to determine the length of the string passed
E.g.:
echo strlen ("Hello");
Output:
5
This PHP string function is used to get the reverse of the string
E.g.:
echo strrev ("welcome");
Output:
emoclew
This returns the position of the first occurrence of a string inside a string
E.g.:
echo strops("There you go", "go");
Output:
11
This repeats a string specified number of times
E.g.:
echo str_repeat ('b', 5);
Output:
bbbbb
This PHP string function finds the specified word, replaces that with a specified word, and return the string
E.g.:
echo str_replace ("great", "wonderful", "have a great day");
Output:
have a wonderful day
This PHP string function inserts html line breaks in front of each new line of the string
E.g.:
echo nl2br ("Lets break \nthe sentence");
Output:
Lets break
the sentence
This calculates the similarity between two strings
E.g.:
echo similar_text ("Hello World","Great World");
Output:
7
This PHP string function writes a formatted string to a variable
E.g.:
echo sprintf ("There are %u wonders in the World",7);
Output:
There are 7 wonders in the World
This replaces characters in the string with specific characters. This function is case insensitive.
E.g.:
echo str_ireplace ("great", "WOW", "This is a great place");
Output:
This is a wow place
This randomly shuffles all characters in a string
E.g.:
echo str_shuffle("Hello World");
Output:
lloeWlHdro
This PHP string function returns the number of words in the given string
E.g.:
echo str_word_count ("a nice day");
Output:
3
This returns the number of characters before the specified character
echo strcspn ("Hello world!","w");
Output:
6
This function is used to pad to the right side of the string, a specified number of characters with the specified character
E.g.:
echo str_pad ("Hello", 10, ".");
Output:
Hello…..
This PHP string function returns the ASCII value of the first character of the string.
E.g.:
echo ord ("hello");
Output:
104
Find the first occurrence of a specified string within a string
E.g.:
echo strchr ("Hello world!", "world");
Output:
world!
This returns the number of characters found in the string that contains characters from the specified string.
E.g.:
echo strspn ("Hello world!", "Hl");
Output:
1
There are few more string functions available in PHP. The above string functions are commonly used functions in PHP for various requirements.
以上是PHP 字串函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!