Advertisement
Guest User

PHP to closes any open LTR/RTL unicode controls

a guest
Apr 4th, 2013
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.89 KB | None | 0 0
  1. function bidi_cleanup($str){
  2.     # Closes all embedded RTL and LTR unicode formatting blocks in a string so that
  3.     # it can be used inside another without controlling its direction.
  4.     # More info: http://www.iamcal.com/understanding-bidirectional-text/
  5.     #
  6.     # LRE - U+202A - 0xE2 0x80 0xAA
  7.     # RLE - U+202B - 0xE2 0x80 0xAB
  8.     # LRO - U+202D - 0xE2 0x80 0xAD
  9.     # RLO - U+202E - 0xE2 0x80 0xAE
  10.     #
  11.     # PDF - U+202C - 0xE2 0x80 0xAC
  12.     #
  13.     $explicits  = '\xE2\x80\xAA|\xE2\x80\xAB|\xE2\x80\xAD|\xE2\x80\xAE';
  14.     $pdf        = '\xE2\x80\xAC';
  15.  
  16.     $stack = 0;
  17.     $str = preg_replace_callback("!(?<explicits>$explicits)|(?<pdf>$pdf)!", function($match) use (&$stack) {
  18.         if (isset($match['explicits']) && $match['explicits']) {
  19.             $stack++;
  20.         } else {
  21.             if ($stack)
  22.                 $stack--;
  23.             else
  24.                 return '';
  25.         }
  26.         return $match[0];
  27.     }, $str);
  28.     for ($i=0; $i<$stack; $i++){
  29.         $str .= "\xE2\x80\xAC";
  30.     }
  31.     return $str;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement