How to copy a string using multibyte-safe fin PHP

5 Answers

0 votes
// Copy using mb_substr()

mb_internal_encoding("UTF-8");

$src = "Programming is fun";

$dest = mb_substr($src, 0);

echo $dest;



/*
run:

Programming is fun

*/

 



answered 18 hours ago by avibootz
0 votes
// Copy using mb_strcut()

mb_internal_encoding("UTF-8");

$src = "Programming is fun";
$dest = mb_strcut($src, 0);

echo $dest;



/*
run:

Programming is fun

*/

 



answered 18 hours ago by avibootz
0 votes
// Copy using mb_convert_encoding()

mb_internal_encoding("UTF-8");

$src = "Programming is fun";
$dest = mb_convert_encoding($src, "UTF-8", "UTF-8");

echo $dest;



/*
run:

Programming is fun

*/

 



answered 18 hours ago by avibootz
0 votes
// Copy using preg_replace()

mb_internal_encoding("UTF-8");

$src = "Programming is fun";
$dest = preg_replace('/^(.+)$/u', '$1', $src);

echo $dest;



/*
run:

Programming is fun

*/

 



answered 18 hours ago by avibootz
0 votes
// Copy using mb_str_split() and implode()

mb_internal_encoding("UTF-8");

$src = "Programming is fun";
$chars = mb_str_split($src);
$dest = implode("", $chars);

echo $dest;



/*
run:

Programming is fun

*/

 



answered 18 hours ago by avibootz
...