Dave's Brain

Browse - Programming Tips - How can I split a UTF-8 string into characters?

Date: 2017sep15
Language: php

Q.  How can I split a UTF-8 string into characters?

A.

// Split a UTF-8 string to characters function utf8Split($str) { return preg_split('//u', $str, null, PREG_SPLIT_NO_EMPTY); } // See if a string has accented letters or other non-ASCII chars function hasMultibyteChars($str) { $charArray = utf8Split($str) { $len = count($charArray); for ($i = 0; $i < $len; $i++) { $char = $charArray[$i]; if (strlen($char) > 1) return true; } return false; }
This is better than using the multibyte functions. Don't forget to add
<meta charset="UTF-8">
to your page.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.