Created
February 8, 2023 22:43
-
-
Save Gori4ka/528e01ad04a442d6ae2c98ee511b85d2 to your computer and use it in GitHub Desktop.
php hebrew reverse
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?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