設為首頁收藏本站

艾歐踢論壇

 找回密碼
 立即註冊

QQ登錄

只需一步,快速開始

搜索
熱搜: 活動 交友 discuz
查看: 809|回復: 0
打印 上一主題 下一主題

10 Useful PHP String-Related Functions

[複製鏈接]
跳轉到指定樓層
樓主
發表於 2016-1-17 11:00:34 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
1. Replace Last Occurrence of a String
  1. /**
  2. * Replace the last occurrence of a string.
  3. *
  4. * @param string $search
  5. * @param string $replace
  6. * @param string $subject
  7. * @return string
  8. */
  9. function strReplaceLast ( $search, $replace, $subject ) {

  10.     $lenOfSearch = strlen( $search );
  11.     $posOfSearch = strrpos( $subject, $search );

  12.     return substr_replace( $subject, $replace, $posOfSearch, $lenOfSearch );

  13. }
複製代碼
2. Strip All Non-Alpha Numeric Characters and Spaces
  1. /**
  2. * Remove all characters except letters, numbers, and spaces.
  3. *
  4. * @param string $string
  5. * @return string
  6. */
  7. function stripNonAlphaNumericSpaces( $string ) {
  8.     return preg_replace( "/[^a-z0-9 ]/i", "", $string );
  9. }
複製代碼
3. Strip All Non-Alpha Numeric Characters
  1. /**
  2. * Remove all characters except letters and numbers.
  3. *
  4. * @param string $string
  5. * @return string
  6. */
  7. function stripNonAlphaNumeric( $string ) {
  8.     return preg_replace( "/[^a-z0-9]/i", "", $string );
  9. }
複製代碼
4. Remove All Non-Numeric Characters
  1. /**
  2. * Remove all characters except numbers.
  3. *
  4. * @param string $string
  5. * @return string
  6. */
  7. function stripNonNumeric( $string ) {
  8.     return preg_replace( "/[^0-9]/", "", $string );
  9. }
複製代碼
5. Remove All Non-Alpha Characters
  1. /**
  2. * Remove all characters except letters.
  3. *
  4. * @param string $string
  5. * @return string
  6. */
  7. function stripNonAlpha( $string ) {
  8.     return preg_replace( "/[^a-z]/i", "", $string );
  9. }
複製代碼
6. Remove All Excess White-Space
  1. /**
  2. * Transform two or more spaces into just one space.
  3. *
  4. * @param string $string
  5. * @return string
  6. */
  7. function stripExcessWhitespace( $string ) {
  8.     return preg_replace( '/  +/', ' ', $string );
  9. }
複製代碼
7. Format a String for a URL Slug
  1. /**
  2. * Format a string so it can be used for a URL slug
  3. *
  4. * @param string $string
  5. * @return string
  6. */
  7. function formatForUrl( $string ) {

  8.     $string = stripNonAlphaNumericSpaces( trim( strtolower( $string ) ) );
  9.     return str_replace( " ", "-", stripExcessWhitespace( $string ) );

  10. }
複製代碼
8. Format an URL Slug into a Human Readable String
  1. /**
  2. * Format a slug into human readable string
  3. *
  4. * @param string $string
  5. * @return string
  6. */
  7. function formatFromUrl( $string ) {
  8.     return str_replace( "-", " ", trim( strtolower( $string ) ) );
  9. }
複製代碼
9. Get Unique Characters from a String
  1. /**
  2. * Get an array of unique characters used in a string. This should also work with multibyte characters.
  3. *
  4. * @param string $string
  5. * @return mixed
  6. */
  7. function getUniqueChars( $string, $returnAsArray=true ) {
  8.     $unique = array_unique( preg_split( '/(?<!^)(?!$)/u', $string ) );
  9.     if ( empty( $returnAsArray ) ) {
  10.         $unique = implode( "", $unique );
  11.     }
  12.     return $unique;
  13. }
複製代碼
10. Generate a Random String
  1. /**
  2. * Generate a random string of specified length from a set of specified characters
  3. *
  4. * @param integer $size Default size is 30 characters.
  5. * @param string $chars The characters to use for randomization.
  6. */
  7. function randomString( $size=30, $chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" ) {

  8.     $string = "";
  9.     $length = strlen( $chars );

  10.     for( $i=0; $i < $size; $i++ ) {
  11.         $string .= $chars{ rand( 0, $length ) };
  12.     }

  13.     return $string;

  14. }
複製代碼











分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 轉播轉播 分享分享 分享淘帖
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

小黑屋|Archiver|手機版|艾歐踢創新工坊    

GMT+8, 2024-5-15 13:21 , Processed in 0.210215 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表