Php string truncation function, php custom string truncation method

Source: Internet
Author: User
Php string truncation function, php custom string truncation method

  1. /**
  2. * Extract database-extract strings
  3. * @ Param string $ string
  4. * @ Param int $ length
  5. * @ Param string $ dot: Specifies whether to add a screenshot after capturing...
  6. * @ Param string $ charset encoding
  7. * @ Return string
  8. */
  9. Public function cutstr ($ string, $ length, $ dot = '...', $ charset = 'utf-8 '){
  10. If (strlen ($ string) <= $ length ){
  11. Return $ string;
  12. }
  13. $ String = str_replace (array ('&', '"', '<', '>'), array ('&', '"', '<', '>'), $ string );
  14. $ Strcut = '';
  15. If (strtolower ($ charset) = 'utf-8 '){
  16. $ N = $ tn = $ noc = 0;
  17. While ($ n <strlen ($ string )){
  18. $ T = ord ($ string [$ n]); // ASCII?
  19. If ($ t = 9 | $ t = 10 | (32 <= $ t & $ t <= 126 )){
  20. $ Tn = 1; $ n ++; $ noc ++;
  21. } Elseif (194 <=$ t & $ t <= 223 ){
  22. $ Tn = 2; $ n + = 2; $ noc + = 2;
  23. } Elseif (224 <=$ t & $ t <239 ){
  24. $ Tn = 3; $ n + = 3; $ noc + = 2;
  25. } Elseif (240 <=$ t & $ t <= 247 ){
  26. $ Tn = 4; $ n + = 4; $ noc + = 2;
  27. } Elseif (248 <=$ t & $ t <= 251 ){
  28. $ Tn = 5; $ n + = 5; $ noc + = 2;
  29. } Elseif ($ t = 252 | $ t = 253 ){
  30. $ Tn = 6; $ n + = 6; $ noc + = 2;
  31. } Else {
  32. $ N ++;
  33. }
  34. If ($ noc >=$ length ){
  35. Break;
  36. }
  37. }
  38. If ($ noc> $ length ){
  39. $ N-= $ tn;
  40. }
  41. $ Strcut = substr ($ string, 0, $ n );
  42. } Else {
  43. For ($ I = 0; $ I <$ length; $ I ++ ){
  44. $ Strcut. = ord ($ string [$ I]) & gt; 127? $ String [$ I]. $ string [++ $ I]: $ string [$ I];
  45. }
  46. }
  47. $ Strcut = str_replace (array ('&', '"', '<', '>'), array ('&', '"', '<', '>'), $ strcut );
  48. Return $ strcut. $ dot;
  49. }

Example 2: php checks whether a string exists:

  1. /**
  2. * KeyStore Library-whether the string exists
  3. * @ Param string $ str: character or string
  4. * @ Param string $ string: string
  5. * @ Return string example: $ str = '34' $ string = '000000' returns TRUE.
  6. */
  7. Public function is_str_exist ($ str, $ string ){
  8. $ String = (string) $ string;
  9. $ Str = (string) $ str;
  10. Return strstr ($ string, $ str) === false? False: true;
  11. }

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.