Posted By

daisuke103 on 02/12/08


Tagged

php textmate html substr count str tag stlen mb


Versions (?)

PHP 文字を切り出す


 / Published in: PHP
 

  1. $substr_test1= "日本語です";
  2. $substr_test2 = "English";
  3. $substr_test3 = "日本語English";
  4.  
  5. //バイト数を指定して文字列を取り出す。
  6. print substr( $substr_test1, 2, 2 ) . "\n"; //本
  7. print substr( $substr_test2, 2, 2 ) . "\n"; //gl
  8. print substr( $substr_test3, 8 ) . "\n"; //glish
  9.  
  10. //バイト数を指定して文字列を取り出す(右側から)。
  11. print substr( $substr_test1, -4, 2 ) . "\n"; //で
  12. print substr( $substr_test2, -4, 2 ) . "\n"; //li
  13. print substr( $substr_test3, -4 ) . "\n"; //lish
  14.  
  15. //文字数を指定して文字列を取り出す。
  16. print mb_substr( $substr_test1, 2, 2, 'UTF-8' ) . "\n"; //語で
  17. print mb_substr( $substr_test2, 2, 2, 'UTF-8' ) . "\n"; //gl
  18. print mb_substr( $substr_test3, 8, 'UTF-8' ) . "\n"; //sh
  19.  
  20. //文字数を指定して文字列を取り出す(右側から)。
  21. print mb_substr( $substr_test1, -4, 2, 'UTF-8' ) . "\n"; //本語
  22. print mb_substr( $substr_test2, -4, 2, 'UTF-8' ) . "\n"; //li
  23. print mb_substr( $substr_test3, -4, 3, 'UTF-8' ) . "\n"; //lish

Report this snippet  

You need to login to post a comment.