|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-10-24 20:07 UTC] adrya dot stembridge at gmail dot com
Description:
------------
I am attempting to break up a string of initials in order to add periods after each letter. Ex, AMA becomes A.M.A. I'm using str_split to generate an array, which I then glue together with periods.
The problem is when I have a certain UTF8 character in the string: "ŞZ". str_split appears to not like the UTF8 encoding of the first letter. I've added some debug output in the code snippet below.
Test script:
---------------
$str_initials = "ŞZ";
echo "<p><b>\$str_initials</b> $str_initials<p>";
$arr_initials = str_split($str_initials);
print_r($arr_initials);
echo "<p>";
foreach ($arr_initials as $initial)
{
echo "<b>\$initial</b> $initial<br>";
$arr_ni[] = $initial . '.';
}
$str_initials = implode($arr_ni);
echo "<p><b>\$str_initials</b> $str_initials";
Expected result:
----------------
ŞZ becomes Ş.Z.
Actual result:
--------------
ŞZ becomes �.�.Z.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 08:00:02 2025 UTC |
I don't normally critique code here but there's a much simpler way to do all that: $str_initials = preg_replace('/./u', '$0.', $str_initials); (UTF-8 only)