Pages

Tuesday, November 30, 2010

PHP substring with UTF-8

You may have noticed strange characters (like �) to appear when you try using the PHP substr function with a UTF8 string. The solution is easy, elegant and core PHP: mb_substr

For example mb_substr("Greek χαρακτήρες",0,10,"UTF8"), will return the first 10 UTF8 characters of our UTF8 string.

For more details see the PHP mb_substr manual.

2 comments:

  1. it works:

    use
    mb_substr("Greek χαρακτήρες",0,10,"UTF8")



    or


    first:

    mb_internal_encoding("UTF-8");

    and after any

    mb_substr("Greek χαρακτήρες",0,10)

    ReplyDelete