PHP學習系列(1)——字串處理函數(4)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   使用   ar   

16、hebrevc() 函數把希伯來文本從右至左的流轉換為左至右的流。它也會把新行 (\n) 轉換為 <br />。只有 224 至 251 之間的 ASCII 字元,以及標點符號受到影響。

文法:hebrev(string,maxcharline)
maxcharline規定每行的最大字元數。如果可能,hebrev() 將避免把單詞斷開。 提示:hebrev() 和 hebrevc() 可以把希伯來邏輯文本轉換為希伯來可見文本。希伯來可見文本不需要特殊的右至左字元支援,這使它對於在 web 上顯示希伯來文本很有用處。

17、htmlspecialchars() 函數把一些預定義的字元轉換為 HTML 實體。

預定義的字元是:

  • & (和號) 成為 &amp;
  • " (雙引號) 成為 &quot;
  • ‘ (單引號) 成為 &#039;
  • < (小於) 成為 &lt;
  • > (大於) 成為 &gt;
文法:htmlspecialchars(string,quotestyle,character-set)

quotestyle——可選。規定如何編碼單引號和雙引號。

  • ENT_COMPAT - 預設。僅編碼雙引號。
  • ENT_QUOTES - 編碼雙引號和單引號。
  • ENT_NOQUOTES - 不編碼任何引號。

character-set——可選。字串值,規定要使用的字元集。

  • ISO-8859-1 - 預設。西歐。
  • ISO-8859-15 - 西歐(增加 Euro 符號以及法語、芬蘭語字母)。
  • UTF-8 - ASCII 相容多位元組 8 位元 Unicode
  • cp866 - DOS 專用 Cyrillic 字元集
  • cp1251 - Windows 專用 Cyrillic 字元集
  • cp1252 - Windows 專用西歐字元集
  • KOI8-R - 俄語
  • GB2312 - 簡體中文,國家標準字元集
  • BIG5 - 繁體中文
  • BIG5-HKSCS - Big5 香港擴充
  • Shift_JIS - 日語
  • EUC-JP - 日語
提示:無法被識別的字元集將被忽略,並由 ISO-8859-1 代替。 例子
<html><body><?php$str = "John & ‘Adams‘";echo htmlspecialchars($str, ENT_COMPAT);echo "<br />";echo htmlspecialchars($str, ENT_QUOTES);echo "<br />";echo htmlspecialchars($str, ENT_NOQUOTES);?></body></html>

瀏覽器輸出:

John & ‘Adams‘John & ‘Adams‘John & ‘Adams‘

如果在瀏覽器中查看原始碼,會看到這些 HTML:

<html><body>John &amp; ‘Adams‘<br />John &amp; &#039;Adams&#039;<br />John &amp; ‘Adams‘</body></html>

 

18、htmlspecialchars_decode() 函數把一些預定義的 HTML 實體轉換為字元,是htmlspecialchars() 的反函數。

文法:htmlspecialchars_decode(string,quotestyle)

quotestyle的具體含義同htmlspecialchars()。

19、implode() 函數把數組元素組合為一個字串。

文法:implode(separator,array)
separator——可選。規定數組元素之間放置的內容。預設是 ""(Null 字元串)。

array——必需。要結合為字串的數組。

說明:雖然 separator 參數是可選的。但是為了向後相容,推薦您使用使用兩個參數。注釋:implode() 可以接收兩種參數順序。但是由於曆史原因,explode() 是不行的。你必須保證 separator 參數在 string 參數之前才行。例子
<?php$arr = array(‘Hello‘,‘World!‘,‘Beautiful‘,‘Day!‘);echo implode(" ",$arr);?>

輸出:

Hello World! Beautiful Day!
20、join() 函數把數組元素組合為一個字串。join() 函數是 implode() 函數的別名。
21、levenshtein() 函數返回兩個字串之間的 Levenshtein 距離。

Levenshtein 距離,又稱編輯距離,指的是兩個字串之間,由一個轉換成另一個所需的最少編輯操作次數。許可的編輯操作包括將一個字元替換成另一個字元,插入一個字元,刪除一個字元。

例如把 kitten 轉換為 sitting:

  1. sitten (k→s)
  2. sittin (e→i)
  3. sitting (→g)

levenshtein() 函數給每個操作(替換、插入和刪除)相同的權重。不過,您可以通過設定可選的 insert、replace、delete 參數,來定義每個操作的代價。

文法:levenshtein(string1,string2,insert,replace,delete)
參數 描述
string1 必需。要對比的第一個字串。
string2 必需。要對比的第二個字串。
insert 可選。插入一個字元的代價。預設是 1。
replace 可選。替換一個字元的代價。預設是 1。
delete 可選。刪除一個字元的代價。預設是 1。

注意:

如果其中一個字串超過 255 個字元,levenshtein() 函數返回 -1。levenshtein() 函數對大小寫不敏感。levenshtein() 函數比 similar_text() 函數更快。不過,similar_text() 函數提供需要更少修改的更精確的結果。

例子
<?phpecho levenshtein("Hello World","ello World");echo "<br />";echo levenshtein("Hello World","ello World",10,20,30);?>

輸出:

130
22、localeconv() 函數返回包含本地數字及貨幣資訊格式的數組。
23、ltrim() 函數從字串左側刪除空格或其他預定義字元。功能類似於chop()或者rtrim();
24、md5() Function Compute字串的 MD5 散列。md5() 函數使用 RSA 資料安全,包括 MD5 報文摘譯演算法。如果成功,則返回所計算的 MD5 散列,如果失敗,則返回 false。
文法:md5( string, raw)

raw——可選。規定十六進位或二進位輸出格式:

  • TRUE - 原始 16 字元二進位格式
  • FALSE - 預設。32 字元十六進位數

注釋:該參數是 PHP 5.0 中添加的。

25、md5_file() Function Compute檔案的 MD5 散列。md5() 函數使用 RSA 資料安全,包括 MD5 報文摘譯演算法。如果成功,則返回所計算的 MD5 散列,如果失敗,則返回 false。

例子 1
<?php$filename = "test.txt";$md5file = md5_file($filename);echo $md5file;?>

輸出:

5d41402abc4b2a76b9719d911017c592
26、metaphone() Function Compute字串的 metaphone 鍵。metaphone 鍵字串的英語發音。metaphone() 函數可用於拼字檢查應用程式。
如果成功,則返回字串的 metaphone 鍵,如果失敗,則返回 false。
文法:metaphone(string,length)

length——可選。規定 metaphone 鍵的最大長度。說明:metaphone() 為發音相似的單詞建立相同的鍵。所產生的 metaphone 鍵長度可變。metaphone() 比 soundex() 函數更精確,因為 metaphone() 瞭解基本的英語發音規則。例子:例子 1

<?phpecho metaphone("world");?>

輸出:

WRLT
例子 2

在本例中,我們對兩個發音相似的單詞應用 metaphone() 函數:

<?php$str = "Sun";$str2 = "Son";echo metaphone($str);echo metaphone($str2);?>

輸出:

SNSN
27、money_format() 函數把字串格式化為貨幣字串。
文法:money_format(string,number)
number——可選。被插入格式化字串中 % 符號位置的數字。注釋:money_format() 函數無法在 windows 平台上工作。例子:例子 1

國際 en_US 格式:

<?php$number = 1234.56;setlocale(LC_MONETARY, "en_US");echo money_format("The price is %i", $number);?>

輸出:

The price is USD 1,234.56
例子 2

負數,帶有 () 指示負數的 US 國際格式,右側精度為 2,"*" 為填充字元:

<?php$number = -1234.5672;echo money_format("%=*(#10.2n", $number);?>

輸出:

($********1,234.57)
28、nl_langinfo() 函數返回指定的本地資訊。

如果成功,則返回指定的本地資訊。如果失敗,則返回 false。文法:nl_langinfo(element)

element——必需。規定要返回哪個元素。必須是說明中列出的元素之一。說明:

時間和日曆:

  • ABDAY_(1-7) - Abbreviated name of the numbered day of the week
  • DAY_(1-7) - Name of the numbered day of the week (DAY_1 = Sunday)
  • ABMON_(1-12) - Abbreviated name of the numbered month of the year
  • MON_(1-12) - Name of the numbered month of the year
  • AM_STR - String for Ante meridian
  • PM_STR - String for Post meridian
  • D_T_FMT - String that can be used as the format string for strftime() to represent time and date
  • D_FMT - String that can be used as the format string for strftime() to represent date
  • T_FMT - String that can be used as the format string for strftime() to represent time
  • T_FMT_AMPM - String that can be used as the format string for strftime() to represent time in 12-hour format with ante/post meridian
  • ERA - Alternate era
  • ERA_YEAR - Year in alternate era format
  • ERA_D_T_FMT - Date and time in alternate era format (string can be used in strftime())
  • ERA_D_FMT - Date in alternate era format (string can be used in strftime())
  • ERA_T_FMT - Time in alternate era format (string can be used in strftime())

貨幣類別:

  • INT_CURR_SYMBOL - Currency symbol (example: USD)
  • CURRENCY_SYMBOL - Currency symbol (example: $)
  • CRNCYSTR - Same as CURRENCY_SYMBOL
  • MON_DECIMAL_POINT - Monetary decimal point character
  • MON_THOUSANDS_SEP - Monetary thousands separator
  • POSITIVE_SIGN - Positive value character
  • NEGATIVE_SIGN -Negative value character
  • MON_GROUPING - Array displaying how monetary numbers are grouped (example: 1 000 000)
  • INT_FRAC_DIGITS - International fractional digits
  • FRAC_DIGITS - Local fractional digits
  • P_CS_PRECEDES - True (1) if currency symbol is placed in front of a positive value, False (0) if it is placed behind
  • P_SEP_BY_SPACE - True (1) if there is a spaces between the currency symbol and a positive value, False (0) otherwise
  • N_CS_PRECEDES - True (1) if currency symbol is placed in front of a negative value, False (0) if it is placed behind
  • N_SEP_BY_SPACE - True (1) if there is a spaces between the currency symbol and a negative value, False (0) otherwise
  • P_SIGN_POSN - Formatting setting. Possible return values:
    • 0 - Parentheses surround the quantity and currency symbol
    • 1 - The sign string is placed in front of the quantity and currency symbol
    • 2 - The sign string is placed after the quantity and currency symbol
    • 3 - The sign string is placed immediately in front of the currency symbol
    • 4 - The sign string is placed immediately after the currency symbol
  • N_SIGN_POSN - Formatting setting. Possible return values:
    • 0 - Parentheses surround the quantity and currency symbol
    • 1 - The sign string is placed in front of the quantity and currency symbol
    • 2 - The sign string is placed after the quantity and currency symbol
    • 3 - The sign string is placed immediately in front of the currency symbol
    • 4 - The sign string is placed immediately after the currency symbol

數字類別:

  • DECIMAL_POINT - Decimal point character
  • RADIXCHAR - Same as DECIMAL_POINT
  • THOUSANDS_SEP - Separator character for thousands
  • THOUSEP - Same as THOUSANDS_SEP
  • GROUPING - Array displaying how numbers are grouped (example: 1 000 000)

通訊類別:

  • YESEXPR - Regex string for matching ‘yes‘ input
  • NOEXPR - Regex string for matching ‘no‘ input
  • YESSTR - Output string for ‘yes‘
  • NOSTR - Output string for ‘no‘

代碼集類別:

  • CODESET Return a string with the name of the character encoding.
提示和注釋

注釋:money_format() 函數無法在 windows 平台上工作。

提示:與返回所有本地格式化資訊的 localeconv() 函數不同,nl_langinfo() 返回指定的資訊。

29、nl2br() 函數在字串中的每個新行 (\n) 之前插入 HTML 分行符號 (<br />)。

文法:nl2br(string)
<?phpecho nl2br("One line.\nAnother line.");?>

輸出:

One line.Another line.

HTML 程式碼:

One line.<br />Another line.
30、number_format() 函數通過千位分組來格式化數字。文法:number_format(number,decimals,decimalpoint,separator)

number——必需。要格式化的數字。如果未設定其他參數,則數字會被格式化為不帶小數點且以逗號 (,) 作為分隔字元。

decimals——可選。規定多少個小數。如果設定了該參數,則使用點號 (.) 作為小數點來格式化數字。

decimalpoint——可選。規定用作小數點的字串。

separator——可選。規定用作千位分隔字元的字串。僅使用該參數的第一個字元。比如 "xyz" 僅輸出 "x"。注釋:如果設定了該參數,那麼所有其他參數都是必需的。注釋:該函數支援一個、兩個或四個參數(不是三個)。例子

<?phpecho number_format("1000000");echo number_format("1000000",2);echo number_format("1000000",2,",",".");?>

輸出:

1,000,0001,000,000.001.000.000,00
 

PHP學習系列(1)——字串處理函數(4)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.