String manipulation-intercept string length code snippet

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.

Intercept String length code snippet:


<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:12PX;" >/** * Intercept length * @param unknown $STR * @param number $start * @param unknown $length * @param string $charset * @param str ing $suffix * @return mixed|string */public static function Getcsubstr ($STR, $start =0, $length, $charset = "Utf-8", $suffix = true) {$charset =empty ($charset)? ' Utf-8 ': $charset; $search =array (' (\\\$[a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]*) (\[[a-za-z0-9\-\.\[\]_\ "\ ' \$\ X7f-\xff]+\]) ', '/< (/?) (script|i?frame|style|html|body|title|link|meta|\?| \%) ([^>]*?) >/isu ',//Remove JavaScript '/(<[^>]*) on[a-za-z] \s*= ([^>]*>)/isu ', '/<[\/\! ' *? [^<>]*?>/si ',//Remove HTML tags '/([\ r \ n]) [\s]+/i ',//remove whitespace characters '/& (quot| #34);/I ',//Replace HTML entity '/& (amp| #38); I ', '/& (lt| #60);/I ', '/& (gt| #62);/I ', '/& (nbsp| #160);/I ', '/& (iexcl| #161);/I ', '/& (cent| #162); I ', '/& (pound| #163);/I ', '/& (copy| #169);/I ', '/&# (\d+);/E ', '/<\?/si ', '/\?>/si '); $replace =array ( ' ([a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]*) ', ' <\\1\\2\\3> ', ' \\1\\2 ', "", "\ r \ n", "," "," "," "," "," "," ",", "", "", "", "", "", "<? ', '?> '); $str =preg_replace ($search, $ Replace, $STR), if (Function_exists ("Mb_substr")) {if (Mb_strlen ($str, $charset) <= $length) {return $str;} $slice =mb_substr ($str, $start, $length, $charset);} else{$re [' utf-8 ']= '/[\x01-\x7f]|[ \xc2-\xdf][\x80-\xbf]| [\xe0-\xef] [\X80-\XBF] {2}| [\xf0-\xff] [\X80-\XBF] {3}/"; $re [' gb2312 ']="/[\x01-\x7f]| [\xb0-\xf7] [\xa0-\xfe]/]; $re [' GBK ']= '/[\x01-\x7f]| [\x81-\xfe] [\x40-\xfe]/]; $re [' Big5 ']= '/[\x01-\x7f]| [\x81-\xfe] ([\x40-\x7e]|\xa1-\xfe]) /";p Reg_match_all ($re [$charset], $str, $match), if (!empty ($match [0]) &&count ($match [0]) <= $length) { return $STR;} $slice =join ("", Array_slice ($match [0], $start, $length));} if ($suffix) {return $slice. " ...";} return $slice;} </span>


Main code Features:
Free to intercept the strings in English and Chinese,
Automatically process HTML code,
HTML AutoComplete (For more information: HTML tag completion for Web content and filtering methods)
Achieve the accuracy of capturing the word count of various encoded content,
Segmentation is automatically differentiated by string encoding.
The main functions apply:
Array_slice-takes a paragraph out of the array, and defaults to reordering and resetting the array's numeric index.

Preg_replace: For more information: PHP using regular expressions to find replacement strings


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-intercept string length code snippet

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.