Skip to content

Instantly share code, notes, and snippets.

@Gori4ka
Created February 8, 2023 22:43
Show Gist options
  • Select an option

  • Save Gori4ka/528e01ad04a442d6ae2c98ee511b85d2 to your computer and use it in GitHub Desktop.

Select an option

Save Gori4ka/528e01ad04a442d6ae2c98ee511b85d2 to your computer and use it in GitHub Desktop.
php hebrew reverse
<?php
function reverseHebrew( $text ) {
$words = array_reverse( explode( ' ', $text ) );
foreach ( $words as $index => $word ) {
if ( isHebrew( $word ) ) {
$words[ $index ] = mbStrRev( $word );
}
}
return join( ' ', $words );
}
function isHebrew( $text ) {
for ( $i = 0, $cnt = strlen( $text ); $i < $cnt; ++ $i ) {
if ( ord( $text[ $i ] ) > 127 ) {
return true;
}
}
return false;
}
function mbStrRev( $string, $encoding = null ) {
if ( $encoding === null ) {
$encoding = mb_detect_encoding( $string );
}
$length = mb_strlen( $string, $encoding );
$reversed = '';
while ( $length -- > 0 ) {
$reversed .= mb_substr( $string, $length, 1, $encoding );
}
return $reversed;
}
echo reverseHebrew('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment