PHP specifies an instance to share a string that contains Chinese and English characters or numbers,

Source: Internet
Author: User

PHP specifies an instance to share a string that contains Chinese and English characters or numbers,

During the development process, we often encounter the problem of intercepting Chinese and English numbers. We all know that Chinese characters are different from English numbers; then, we will write some daily functions in the project's common, including the time calculation conversion and the functions intercepted by Chinese and English characters; for example, I'm afraid I can't take a few lines of code for intercepting Chinese and English functions. Now I want to tell you a simple one (I have run it locally). If you have any questions, please kindly advise.
 

$ C = 'ddaabbccaa '; $ d = 'Earth needs the love of each of us'; $ frist1 = mb_substr ($ c, 0, 1, "UTF-8 "); // d $ delete_last1 = mb_substr ($ d,-, "UTF-8"); // echo $ frist1. '++ '. $ delete_last1. '<br/>'; // d ++ $ frist2 = mb_substr ($ d, 0, 1, "UTF-8 "); // $ delete_last2 = mb_substr ($ d,-, "UTF-8"); // echo $ frist2. '++ '. $ delete_last2. '<br/>'; // ground ++ $ e = '11aa22cc33 '; $ f = 'aa Earth needs the love of each of us '; $ g = 'Earth needs the love of each of us gg'; $ h = 'Earth needs the love of each of us '; $ first3 = mb_substr ($ e, 0, 1, "UTF-8"); // 1 $ last3 = mb_substr ($ f, 0, 1, "UTF-8"); // a $ delete_last3 = mb_substr ($ f, "UTF-8"); // $ e_last4 = mb_substr ($ g,-, "UTF-8"); // g $ frist4 = mb_substr ($ g, 0, 1, "UTF-8"); // $ delete_last5 = mb_substr ($ h,-, "UTF-8"); // echo $ first3. '+ + + '. $ last3 .'---'. $ delete_last3. '***'. $ delete_last4. '&&&'. $ frist4. '<br/>'; // 1 ++ a --- protection *** g & amp; echo $ last3 .'... '. $ delete_last3. '<br/>'; //... echo $ frist4 .'... '. $ delete_last5. '<br/>'; // location... this way, no matter whether the string contains Chinese and English numbers, you do not need to judge, for example, "location... protection "or" location... "or"... protection"

PS: used to filter spaces in a string

To remove spaces between the beginning and end of a string:

function mbTrim($str)  {    return mb_ereg_replace('(^( | )+|( | )+$)', '', $str);  } 


During the following regular expression verification, consecutive spaces entered by the user are filtered out, including fullwidth spaces and halfwidth spaces.

$user = mb_ereg_replace('^( | )+', '', $user);$user = mb_ereg_replace('( | )+$', '', $user);$age = mb_ereg_replace('^( | )+', '', $age);$age = mb_ereg_replace('( | )+$', '', $age); $method = mb_ereg_replace('^( | )+', '', $method);$method = mb_ereg_replace('( | )+$', '', $method);$address = mb_ereg_replace('^( | )+', '', $address);$address = mb_ereg_replace('( | )+$', '', $address); 

Articles you may be interested in:
  • Compile a PHP program to check the number of Chinese characters in the string for instance sharing
  • PHP Echo string connection format
  • PHP uses arrays to replace matching items in strings in sequence
  • How to Use the substr_count () function in PHP to obtain the occurrence times of a substring
  • The strncmp () function in PHP compares the first two characters of two strings to determine whether they are equal.
  • In PHP, The strnatcmp () function "natural sorting algorithm" is used to analyze the usage of string comparison (compared with the strcmp function)
  • Comparison and usage of strcmp () and strcasecmp () functions in PHP
  • Analysis on the use of substr function string Truncation in PHP
  • In PHP, addslashes () and stripslashes () are used for String Conversion and restoration.
  • PHP encryption and decryption string function source code download

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.