PHP Learning Series (1) —— string processing function (4), PHP function _php Tutorial

Source: Internet
Author: User
Tags chop crc32 first string key string md5 hash rtrim time and date

PHP Learning Series (1)--String processing function (4), PHP function


16. The HEBREVC () function converts a right-to-left stream of Hebrew text to a left-to-right stream. It will also convert the new line (\ n) to
。 Only ASCII characters between 224 and 251, as well as punctuation marks, are affected.

Syntax: Hebrev (string,maxcharline)
MAXCHARLINE Specifies the maximum number of characters per line. If possible, Hebrev () will avoid breaking the word.

Tip: Hebrev () and HEBREVC () can convert Hebrew logical text to Hebrew visible text. Hebrew visible text does not require special right-to-left character support, which makes it useful for displaying Hebrew text on the web.

17. The Htmlspecialchars () function converts some pre-defined characters to HTML entities.

The predefined characters are:

    • & (and number) becomes &
    • "(double quotes) becomes"
    • ' (single quote) become '
    • < (less than) becomes <
    • > (greater than) becomes >

Syntax: Htmlspecialchars (String,quotestyle,character-set)

quotestyle--is optional. Specifies how to encode single and double quotation marks.

    • Ent_compat-Default. Encodes only double quotes.
    • Ent_quotes-encodes double and single quotation marks.
    • Ent_noquotes-do not encode any quotation marks.

character-set--is optional. A string value that specifies the character set to use.

    • Iso-8859-1-Default. Western europe.
    • Iso-8859-15-Western Europe (added Euro symbol and French, Finnish alphabet).
    • UTF-8-ASCII-compatible multi-byte 8-bit Unicode
    • Cp866-dos dedicated Cyrillic Character Set
    • Cp1251-windows dedicated Cyrillic Character Set
    • Cp1252-windows Special Western European character set
    • Koi8-r-Russian
    • GB2312-Simplified Chinese, national standard character set
    • BIG5-Traditional Chinese
    • Big5-hkscs-big5 Hong Kong expansion
    • Shift_JIS-Japanese
    • EUC-JP-Japanese

Hint: A character set that cannot be recognized is ignored and replaced by Iso-8859-1.

Example

< HTML > < Body > 
  
    php$str = "John & ' Adams '"; Echo htmlspecialchars ($str, Ent_compat); echo "
Echo Htmlspecialchars ($str, ent_quotes);
"; Echo htmlspecialchars ($str, ent_noquotes); ?> body> html>

Browser output:

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

If you view the source code in a browser, you will see these HTML:

< HTML > < Body >  & ' Adams '</>& ' Adams ' </>&  ' Adams '
  
    Body  >
   
     html>  

18. The Htmlspecialchars_decode () function converts some predefined HTML entities to characters and is an inverse function of htmlspecialchars ().

Syntax: Htmlspecialchars_decode (String,quotestyle)

The specific meaning of Quotestyle with Htmlspecialchars ().

19. The implode () function combines array elements into a single string.

Syntax: Implode (Separator,array)
separator--is optional. Specifies what is placed between the elements of the array. The default is "" (an empty string).

array--required. An array to combine as a string.

Description: Although the separator parameter is optional. However, for backwards compatibility, it is recommended that you use two parameters.

Note: Implode () can receive two parameter sequences. But for historical reasons, explode () is not working. You must ensure that the separator parameter is not preceded by a string parameter.

Example

 
  PHP$arrarray(' Hello ', ' world! ', ' Beautiful ', ' day! ' ); Echo implode ("",$arr);? >

Output:

Hello world! Beautiful day!
20. The join () function combines the array elements into a single string. The join () function is an alias for the implode () function.
21, the Levenshtein () function returns the Levenshtein distance between two strings.

Levenshtein distance, also known as the editing distance, refers to the minimum number of edit operations required between two strings, converted from one to another. Permission edits include replacing one character with another character, inserting a character, and deleting a character.

For example, convert kitten to sitting:

The Levenshtein () function gives the same weight for each operation (replace, insert, and delete). However, you can define the cost of each operation by setting the optional Insert, replace, and delete parameters.

Syntax: Levenshtein (String1,string2,insert,replace,delete)

Parameters Description
String1 Necessary. The first string to compare.
string2 Necessary. The second string to compare.
Insert Optional. The cost of inserting a character. The default is 1.
Replace Optional. The cost of replacing a character. The default is 1.
Delete Optional. The cost of deleting a character. The default is 1.

Attention:

If one of the strings exceeds 255 characters, the Levenshtein () function returns-1. The Levenshtein () function is not case sensitive. The Levenshtein () function is faster than the Similar_text () function. However, the Similar_text () function provides more precise results that require less modification.

Example

 
  PHPecholevenshtein("Hello World", "Ello World"); Echo "
"; Echo Levenshtein ("Hello World", "Ello World", 10,20,30);? >

Output:

130
22. The Localeconv () function returns an array containing the local numeric and currency information formats.
23. The LTrim () function removes spaces or other predefined characters from the left side of the string. Functions similar to chop () or RTrim ();
24. The MD5 () function computes the MD5 hash of the string. The MD5 () function uses RSA data security, including the MD5 message selected passage algorithm. If successful, returns the computed MD5 hash, or False if it fails.

Syntax: MD5 (string,raw)

raw-- is optional. Specify hexadecimal or binary output formats:

    • TRUE-Original 16 character binary format
    • FALSE-Default. 32-character hexadecimal number

Note: This parameter is added in PHP 5.0.

25. The Md5_file () function computes the MD5 hash of the file. The MD5 () function uses RSA data security, including the MD5 message selected passage algorithm. If successful, returns the computed MD5 hash, or False if it fails.

Example 1

 
  php$filename = "Test.txt"; $MD 5file Md5_file ($filename); Echo $MD 5file ;? >

Output:

5d41402abc4b2a76b9719d911017c592
26. The Metaphone () function computes the Metaphone key of the string. Metaphone The English pronunciation of the key string. The Metaphone () function can be used for spell-checking applications.
If successful, returns the Metaphone key of the string, or False if it fails.

Syntax: Metaphone (string,length)

length--is optional. Specifies the maximum length of the Metaphone key.

Description: Metaphone () creates the same key for words with similar pronunciation. The resulting Metaphone key length is variable. Metaphone () is more accurate than the SOUNDEX () function because Metaphone () understands basic English pronunciation rules.

Example:

Example 1

 
  PHPechometaphone("World"); >

Output:

Wrlt

Example 2

In this example, we apply the Metaphone () function to two words that are similar in pronunciation:

 
  php$str = "Sun"; $str 2 = "Son"; Echo Metaphone ($str); Echo Metaphone ($str 2);? >

Output:

Snsn
27. The Money_format () function formats the string as a currency string.

Syntax: Money_format (String,number)
number--is optional. The number that is inserted in the formatted string as the% symbol position.

Note: the Money_format () function does not work on the Windows platform.

Example:

Example 1

International en_US Format:

 
  php$number = 1234.56; setlocale (Lc_monetary, "en_US"); Echo $number );? >

Output:

The price is USD 1,234.56

Example 2

Negative numbers, with a () indicates a negative number in US international format, the right precision is 2, "*" is the fill character:

 
  php$number = -1234.5672; Echo $number );? >

Output:

($********1,234.57)
28, the Nl_langinfo () function returns the specified local information.

If successful, the specified local information is returned. If it fails, it returns false.

Syntax: Nl_langinfo (Element)

element--required. Specifies which element to return. Must be one of the elements listed in the description.

Description

Time and calendar:

  • 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 is used as the format String for strftime () to represent time and date
  • D_fmt-string that can is used as the format String for strftime () to represent date
  • T_fmt-string that can is used as the format String for strftime () to represent time
  • T_fmt_ampm-string that can is used as the format String for strftime () to represent time in 12-hour format with Ante/pos T Meridian
  • Era-alternate era
  • Era_year-year in Alternate ERA format
  • Era_d_t_fmt-date and time in alternate era format (string can is used in strftime ())
  • Era_d_fmt-date in Alternate era format (string can is used in strftime ())
  • Era_t_fmt-time in Alternate era format (string can is used in strftime ())

Currency Category:

  • 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 is 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 was placed in front of the quantity and currency symbol
    • 2-the sign string was placed after the quantity and currency symbol
    • 3-the sign string was placed immediately in front of the currency symbol
    • 4-the sign string was 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 was placed in front of the quantity and currency symbol
    • 2-the sign string was placed after the quantity and currency symbol
    • 3-the sign string was placed immediately in front of the currency symbol
    • 4-the sign string was placed immediately after the currency symbol

Number Category:

    • 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 is grouped (Example:1 000 000)

Type of communication:

    • Yesexpr-regex string for matching ' yes ' input
    • Noexpr-regex string for matching ' no ' input
    • Yesstr-output string for ' yes '
    • Nostr-output string for ' no '

Code Set Category:

    • CodeSet Return A string with the name of the character encoding.

Hints and Notes

Note: the Money_format () function does not work on the Windows platform.

Tip: Unlike the localeconv () function, which returns all local formatting information, Nl_langinfo () returns the specified information.

29. The NL2BR () function inserts an HTML newline character before each new line (\ n) in the string (
)。

Syntax: NL2BR (String)

 
  PHPechonl2br("One line.\nanother line.") );? >

Output:

One line. Another line.

HTML Code:

One line. <  />another line.
30. The Number_format () function formats numbers by using thousands of groupings.

Syntax: Number_format (number,decimals,decimalpoint,separator)

number--required. The number to format. If no other parameters are set, the number is formatted with no decimal point and comma (,) as the delimiter.

decimals--is optional. How many decimal places are specified. If this parameter is set, the number is formatted with a dot (.) as the decimal point.

decimalpoint--is optional. Specifies a string to use as a decimal point.

separator--is optional. Specifies the string to use as the thousand separator. Only the first character of the parameter is used. For example, "XYZ" only outputs "X". Note: If this parameter is set, all other parameters are required.

Note: The function supports one, two, or four parameters (not three).

Example

  
   Phpecho Number_format ("1000000"); Echo Number_format ("1000000", 2); Echo Number_format ("1000000", 2, ",", "."); ?>

Output:

1,000,0001,000,000.001.000.000,00

How PHP handles Strings

Through the study of PHP, you can use this high-level language to create a higher performance site. For beginners, for PHP string mbstring is still relatively unfamiliar, let us introduce the PHP string mbstring specific application.

Multi-lingual coexistence means multibyte, PHP's built-in string-length function strlen cannot handle Chinese strings correctly, it only gets the number of bytes that the string occupies. For the Chinese encoding of GB2312, the value of strlen is twice times the number of Chinese characters, and for UTF-8 encoded in Chinese, it is the difference of multiple times.

Using PHP string mbstring can be a good solution to this problem. The usage of Mb_strlen is similar to strlen, except that it has a second optional parameter for specifying character encoding. For example get UTF-8 string $str length, can be used Mb_strlen ($str, ' utf-8′). If you omit the second argument, the internal encoding of PHP is used. The internal code can be obtained through the mb_internal_encoding () function, which is set in two ways:

1. Set mbstring.internal_encoding = UTF-8 in php.ini

2. Call Mb_internal_encoding ("GBK")

In addition to PHP string mbstring, there are a lot of cutting functions, where mb_substr is to divide characters by word, and mb_strcut is to divide characters by Byte, but none of them produce a half-character phenomenon. And from function cutting to the effect of the length is also different, mb_strcut cutting condition is less than strlen, Mb_substr is equal to strlen, see the following example,

<? $str = ' I am a long string of Chinese-www.jefflei.com '; echo "MB_SUBSTR:". Mb_substr ($str, 0, 6, ' utf-8′); echo ""; echo "Mb_strcut:". Mb_strcut ($str, 0, 6, ' utf-8′);?>

The output is as follows:

MB_SUBSTR: I'm a bunch of comparisons

Mb_strcut: I am

It is important to note that PHP string mbstring is not a PHP core function, but it is necessary to ensure that mbstring support is added to PHP when compiling the module:

(1) Use –enable-mbstring at compile time

(2) Modify/usr/local/lib/php.inc

Default_charset = "ZH-CN"

Mbstring.language = ZH-CN

Mbstring.internal_encoding =ZH-CN

PHP string mbstring Class library content is more, also including Mb_ send_ mail such as email processing functions, etc.

(100 points) [PHP] Write some of your familiar string handler functions!

Addcslashes addslashes bin2hex Chop CHR chunk_split convert_cyr_string Cyrillic
Convert_uudecode convert_uuencode count_chars crc32 crc32 crypt echo explode

fprintf get_html_translation_table Hebrev

Hebrevc
Hex2bin-decodes a hexadecimally encoded binary string
Html_entity_decode-convert all HTML entities to their applicable characters
Htmlentities-convert all applicable characters to HTML entities
Htmlspecialchars_decode-convert Special HTML entities back to characters
Htmlspecialchars-convert special characters to HTML entities
Implode-join array elements with a string
Join

Lcfirst-make A string ' s first character lowercase
Levenshtein-calculate Levenshtein distance between, strings
Localeconv-get Numeric formatting information
Ltrim-strip whitespace (or other characters) from the beginning of a string
Md5_file
Metaphone-calculate the Metaphone key of a string
Money_format-formats a number as a currency string
Nl_langinfo-query Language and locale information
Nl2br

Number_format-format a number with grouped thousands
Ord

Parse_str

Print

Printf

Quoted_printable_decode-convert a quoted-printable string to an 8 bit string
Quoted_printable_encode-convert a 8 bit string to a quoted-printable string
Quotemeta-quote Meta characters
RTrim
Setlocale-set locale Information
Sha1_file

Sha1

Soundex-calculate the Soundex key of a string
Sprintf-return a formatted string
Sscanf-parses input from a string according to a ... Remaining full text >>

http://www.bkjia.com/PHPjc/891600.html www.bkjia.com true http://www.bkjia.com/PHPjc/891600.html techarticle PHP Learning Series (1) String processing function (4), PHP function 16, HEBREVC () function convert Hebrew text from right to left stream to left to right stream. It will also convert the new line (\ n) to ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.