PHP指定截取字串中的中英文或數字字元的執行個體分享,_PHP教程

來源:互聯網
上載者:User

PHP指定截取字串中的中英文或數字字元的執行個體分享,


我們在開發過程中,經常會碰到截取中文英文數字等的問題,大家知道中文所佔的字元和英文數字是不同的;然後我們就會在項目的common裡寫一些日常的函數包含時間計算轉換和中英文字元截取的函數;比如截取中英文的函數沒個幾行代碼恐怕拿不下來的吧,現在就告訴大家一個簡單的(自己本地運行過的),如有問題,請大家多多指教

$c = 'ddaabbccaa';$d = '地球需要我們每個人的愛護'; $frist1 = mb_substr( $c, 0, 1 ,"UTF-8"); // d$delete_last1 = mb_substr($d, -1,1,"UTF-8");  // 護echo $frist1.'+++'.$delete_last1.'
'; // d+++護 $frist2 = mb_substr( $d, 0, 1 ,"UTF-8"); // 地$delete_last2 = mb_substr($d, -1,1,"UTF-8"); // 護echo $frist2.'+++'.$delete_last2.'
'; // 地+++護 $e = '11aa22cc33';$f = 'aa地球需要我們每個人的愛護';$g = '地球需要我們每個人的愛護gg';$h = '地球需要我們每個人的愛護';$first3 = mb_substr( $e, 0, 1 ,"UTF-8"); // 1$last3 = mb_substr( $f, 0, 1 ,"UTF-8"); // a$delete_last3 = mb_substr($f, -1,1,"UTF-8"); // 護$delete_last4 = mb_substr($g, -1,1,"UTF-8"); // g$frist4 = mb_substr( $g, 0, 1 ,"UTF-8"); // 地$delete_last5 = mb_substr($h, -1,1,"UTF-8"); // 護 echo $first3.'+++'.$last3.'---'.$delete_last3.'***'.$delete_last4.'&&&'.$frist4.'
'; // 1+++a---護***g&&&地echo $last3.'...'.$delete_last3.'
'; // a...護echo $frist4.'...'.$delete_last5.'
'; // 地...護// 這樣不管字串裡是中英文數字等都是可以的無需判斷,如: ”地...護“ 或者 “地...” 或者 “...護”

PS:過濾字串中空格的方法

去除字串首尾中英文空格的方法:

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


下面的正則驗證時過濾掉使用者輸入的連續空格,包括全形空格和半形空格

$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); 

您可能感興趣的文章:

  • 編寫PHP程式檢查字串中的中文字元個數的執行個體分享
  • PHP Echo字串的串連格式
  • PHP使用數組依次替換字串中匹配項
  • PHP中substr_count()函數擷取子字串出現次數的方法
  • PHP中strncmp()函數比較兩個字串前2個字元是否相等的方法
  • PHP中strnatcmp()函數“自然排序演算法”進行字串比較用法分析(對比strcmp函數)
  • PHP中strcmp()和strcasecmp()函數字串比較用法分析
  • PHP中substr函數字串截取用法分析
  • PHP中addslashes()和stripslashes()實現字串轉義和還原用法執行個體
  • 詳解 PHP加密解密字串函數附源碼下載

http://www.bkjia.com/PHPjc/1111894.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1111894.htmlTechArticlePHP指定截取字串中的中英文或數字字元的執行個體分享, 我們在開發過程中,經常會碰到截取中文英文數字等的問題,大家知道中文所佔的字...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.