String manipulation-Various string format output conversions

Source: Internet
Author: User

Some of the commonly used string manipulation features in projects include: intercept string lengths, output conversions, random strings, full-width half-width conversions, character set conversions, mailbox format validation, and digital to file size.

Various string format output conversion Code Snippets:
/** * Output Conversion * @param unknown $string * @param string $esc _type * @param string $char _set * @return String|mixed|unknown */p Ublic static function Fescape ($string, $esc _type= ' html ', $char _set= ' iso-8859-1 ') {switch ($esc _type) {case ' HTML ':// Special characters converted to HTML entity return Htmlspecialchars ($string, ent_quotes, $char _set); case ' Htmlall ': Return htmlentities ($string, ENT _quotes, $char _set); case ' URL ': Return Rawurlencode ($string); case ' Urlpathinfo ': Return str_replace ('%2f ', '/', Rawurlencode ($string)); case ' quotes '://Escape single quote return preg_replace ("% (? <!\\\\) '%", "\ \" ", $string); case ' hex ':// Converts binary data to hexadecimal notation $return= '; for ($x =0; $x <strlen ($string); $x + +) {$return. = '% '. Bin2Hex ($string [$x]);} return $return; case ' hexentity ': $return = "; for ($x =0; $x <strlen ($string); $x + +) {$return. = ' & #x '. Bin2Hex ($ string[$x]). '; ';} return $return; case ' decentity ': $return = "; for ($x =0; $x <strlen ($string); $x + +) {$return. = '". Ord "$string [$x ]).';';} return $return; case ' JavaScript '://Escape quotes and backslashes, line breaks, and so on. Return Strtr ($string, array (' \ \ ' = ' \\\\ ', "' = ' = ' \ ' ', ' ' ' ' ' \ \ ' \ ', ' \ r ' = ' \\r ', ' \ n ' = ' \\n ', ' </' = ' <\/'); case ' mail ':// Secure way to display the e-mail address on the webpage return str_replace (Array (' @ ', '. '), Array (' [at] ', ' [DOT] '), $string); case ' NONSTD '://escape non-standard characters, such as document reference $_res= '; for ($_i=0,$_len=strlen ($string); $_i<$_len;$_i++) {$_ord=ord (substr ($string, $_i,1));//Non-standard Char, Escape itif ($_ord>=126) {$_res.= '. $_ord. '; ';} Else{$_res.=substr ($string, $_i,1);}} return $_res;default:return $string;}}



Main code features:
Various string format output conversions.
The main functions apply:
Htmlspecialchars: Takes a paragraph out of the array, and defaults to reordering and resetting the array's numeric index.
Htmlentities: For more information, see: PHP uses regular expressions to find replacement strings http://blog.csdn.net/websites/article/details/18734583
Rawurlencode: Returns a string in which all non-alphanumeric characters except-_ are replaced with a percent (%) followed by a two-digit hexadecimal number. URLs are encoded according to RFC 1738, in order to protect the literal characters from being interpreted as a special URL delimiter, while protecting the URL format from being confused by the transfer media (like some messaging systems) using character conversions.
Bin2Hex: Converts binary data to hexadecimal representation
STRTR: Converts the specified character

Ord: Returns the ASCII value of the character, which is a complementary function of Chr ().

preg_replace: For details, please refer to: PHP uses regular expressions to find replacement strings

HTML AutoComplete (for more information: HTML tag completion for Web content and Filtering methods )


Code snippet in continuous update ... If you don't have what you need, you can leave a comment and tell your needs.

Thank you for your interest in websites blog.


String manipulation-Various string format output conversions

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.