Ik zit met een probleem, ik wil mb_substr gebruiken zonder lengt param (dus de hele string), maar met een encoding parameter. Dit krijg ik niet werken, behalve met een ranzige bypass. Dit is wat ik geprobeerd heb:
En de output:
Ik zie vast iets heel simpels over het hoofd, maar ik weet niet wat
Wie weet de oplossing?
PHP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| //Just to be sure, encode it in UTF-8 (test purposes only) $string = utf8_encode("abcdefghijklmnopqrstuvwxyz"); //Trying to get mb_substr() return a string like $string, without the first 4 chars (encoded in UTF-8 $teststring[] = mb_substr($string, 4, NULL, "UTF-8"); //Returns empty string $teststring[] = mb_substr($string, 4, 0, "UTF-8"); //Returns empty string $teststring[] = mb_substr($string, 4, false, "UTF-8"); //Returns empty string //$teststring[] = mb_substr($string, 4, , "UTF-8"); //This won't work at all $teststring[] = mb_substr($string, 4, "UTF-8"); //Returns empty string $teststring[] = mb_substr($string, 4, -1, "UTF-8"); //Returns the right string, exept that the last char is missing $teststring[] = mb_substr($string, 4, mb_strlen($string, "UTF-8") - 4, "UTF-8"); //Works, but can't it be done more easy? $reference = substr("abcdefghijklmnopqrstuvwxyz", 4); $length = strlen($reference); print "Reference: <br> The test should return a string with $length characters: $reference <br><br>"; foreach($teststring as $key => $value) { $length = mb_strlen($value, "UTF-8"); print "Test $key returned a string with $length characters: $value<br>"; } |
En de output:
code:
1
2
3
4
5
6
7
8
9
| Reference: The test should return a string with 22 characters: efghijklmnopqrstuvwxyz Test 0 returned a string with 0 characters: Test 1 returned a string with 0 characters: Test 2 returned a string with 0 characters: Test 3 returned a string with 0 characters: Test 4 returned a string with 21 characters: efghijklmnopqrstuvwxy Test 5 returned a string with 22 characters: efghijklmnopqrstuvwxyz |
Ik zie vast iets heel simpels over het hoofd, maar ik weet niet wat