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

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

語法:hebrev(string,maxcharline)
maxcharline規定每行的最大字符數。若是可能,hebrev() 將避免把單詞斷開。

提示:hebrev() 和 hebrevc() 能夠把希伯來邏輯文本轉換爲希伯來可見文本。希伯來可見文本不須要特殊的右至左字符支持,這使它對於在 web 上顯示希伯來文本頗有用處。

1七、htmlspecialchars() 函數把一些預約義的字符轉換爲 HTML 實體。html

預約義的字符是:git

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

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

  • 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>

瀏覽器輸出:windows

John & 'Adams'
John & 'Adams'
John & 'Adams'

若是在瀏覽器中查看源代碼,會看到這些 HTML:數組

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

 

1八、htmlspecialchars_decode() 函數把一些預約義的 HTML 實體轉換爲字符,是htmlspecialchars() 的反函數。瀏覽器

語法:htmlspecialchars_decode(string,quotestyle)安全

quotestyle的具體含義同htmlspecialchars()。函數

1九、implode() 函數把數組元素組合爲一個字符串。

語法:implode(separator,array)
separator——可選。規定數組元素之間放置的內容。默認是 ""(空字符串)。

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

說明:雖然 separator 參數是可選的。可是爲了向後兼容,推薦您使用使用兩個參數。

註釋:implode() 能夠接收兩種參數順序。可是因爲歷史緣由,explode() 是不行的。你必須保證 separator 參數在 string 參數以前才行。

例子

<?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>

輸出:

Hello World! Beautiful Day!
20、join() 函數把數組元素組合爲一個字符串。join() 函數是 implode() 函數的別名。
2一、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() 函數提供須要更少修改的更精確的結果。

例子

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

輸出:

1
30
2二、localeconv() 函數返回包含本地數字及貨幣信息格式的數組。
2三、ltrim() 函數從字符串左側刪除空格或其餘預約義字符。功能相似於chop()或者rtrim();
2四、md5() 函數計算字符串的 MD5 散列。md5() 函數使用 RSA 數據安全,包括 MD5 報文摘譯算法。若是成功,則返回所計算的 MD5 散列,若是失敗,則返回 false。
語法:md5(string,raw)

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

  • TRUE - 原始 16 字符二進制格式
  • FALSE - 默認。32 字符十六進制數

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

2五、md5_file() 函數計算文件的 MD5 散列。md5() 函數使用 RSA 數據安全,包括 MD5 報文摘譯算法。若是成功,則返回所計算的 MD5 散列,若是失敗,則返回 false。

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

輸出:

5d41402abc4b2a76b9719d911017c592
2六、metaphone() 函數計算字符串的 metaphone 鍵。metaphone 鍵字符串的英語發音。metaphone() 函數可用於拼寫檢查應用程序。
若是成功,則返回字符串的 metaphone 鍵,若是失敗,則返回 false。
語法:metaphone(string,length)

length——可選。規定 metaphone 鍵的最大長度。

說明:metaphone() 爲發音類似的單詞建立相同的鍵。所生成的 metaphone 鍵長度可變。metaphone() 比 soundex() 函數更精確,由於 metaphone() 瞭解基本的英語發音規則。

例子:

例子 1
<?php
echo metaphone("world");
?>

輸出:

WRLT
例子 2

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

<?php
$str = "Sun";
$str2 = "Son";

echo metaphone($str);
echo metaphone($str2);
?>

輸出:

SN
SN
2七、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)
2八、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() 返回指定的信息。

2九、nl2br() 函數在字符串中的每一個新行 (\n) 以前插入 HTML 換行符 (<br />)。

語法:nl2br(string)
<?php
echo 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"。註釋:若是設置了該參數,那麼全部其餘參數都是必需的。

註釋:該函數支持一個、兩個或四個參數(不是三個)。

例子

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

輸出:

1,000,000
1,000,000.00
1.000.000,00
相關文章
相關標籤/搜索