Skip to content

Instantly share code, notes, and snippets.

@julp
Created October 1, 2012 15:54
Show Gist options
  • Select an option

  • Save julp/3812648 to your computer and use it in GitHub Desktop.

Select an option

Save julp/3812648 to your computer and use it in GitHub Desktop.
Couper le texte sur le mot le plus proche pour un extrait (début) de texte limité à X graphèmes
<?php
function cut_on_nearest_word_at($string, $length = 150) {
$start = grapheme_substr($string, 0, $length);
$byte_offset = strlen($start);
$ubrk = IntlBreakIterator::createWordInstance(ini_get('intl.default_locale'));
$ubrk->setText($string);
$byte_offset = $ubrk->preceding($byte_offset);
return substr($string, 0, $byte_offset);
}
$N0="\xF0\x9D\x9F\x8E"; # 1D7CE, Nd
$N1="\xF0\x9D\x9F\x8F";
$N2="\xF0\x9D\x9F\x90";
$N3="\xF0\x9D\x9F\x91";
$N4="\xF0\x9D\x9F\x92";
$N5="\xF0\x9D\x9F\x93";
$N6="\xF0\x9D\x9F\x94";
$N7="\xF0\x9D\x9F\x95";
$N8="\xF0\x9D\x9F\x96";
$N9="\xF0\x9D\x9F\x97";
var_dump(
cut_on_nearest_word_at(
str_repeat("$N1$N2$N3$N4$N5$N6$N7$N8$N9 ", 10),
33
)
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment