How to deal with multi-byte UTF-8 strings in JavaScript and fix the empty delimiter/separator issue


For this, at first use preg_split() and set the function with this −

'//u'

After setting like above, the empty delimiter issue will get fixed, since we added '//u' above.

Example

The PHP code is as follows

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
$stringValues="";
$result= preg_split('//u', $stringValues,-1, PREG_SPLIT_NO_EMPTY);
print_r($result);
echo "<br>";
$stringValues1="John Smith";
$result1= preg_split('//u', $stringValues1,-1, PREG_SPLIT_NO_EMPTY);
print_r($result1);
?>
</body>
</html>

Output

This will produce the following output −

Array ( )
Array ( [0] => J [1] => o [2] => h [3] => n [4] => [5] => S [6] => m [7] => i [8] => t [9] => h )

Updated on: 13-Oct-2020

149 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements