Skip to content

Instantly share code, notes, and snippets.

@douglasmiranda
Created August 24, 2011 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save douglasmiranda/1169157 to your computer and use it in GitHub Desktop.
Save douglasmiranda/1169157 to your computer and use it in GitHub Desktop.
How to get substring from accented words (UTF-8)
<?php
/**
* How to get substring UTF-8 characters
*/
// If you try something like this
$utf_8_characters = 'Sábado';
echo substr($utf_8_characters, 0, 3);
// you get something like "Sá", but not "Sáb"
// now, if you just use mb_substr
// http://www.php.net/manual/en/function.mb-substr.php
echo mb_substr($utf_8_characters, 0, 3, 'UTF-8');
// ..and we get this time: "Sáb"
// Now, we ok :)
// NOTE: For more about Multibyte String
// Functions: http://www.php.net/manual/en/ref.mbstring.php
?>
@belabed90
Copy link

Thank you :)

@renan-arrieiro
Copy link

You save my life

@SebLuca
Copy link

SebLuca commented May 6, 2024

Excellent. Thanks for that :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment