The problem: visual order vs. logical order
Hebrew and Arabic are written and read right-to-left. In memory, Unicode stores them in logical order - i.e. reading order. The trouble starts when software (often PDF engines, legacy systems, or graphics output libraries) stores the characters in the order they are painted on screen - the right-to-left run becomes a left-to-right byte sequence. That is "visual order". When another system reads that sequence and assumes it is logical, the Hebrew shows up reversed.
The fix for the common case is a controlled flip, which is exactly the inverse of the Unicode bidi algorithm for a single paragraph:
- If a line contains no RTL character at all, leave it untouched (so a lone phone number is not "corrected" the wrong way).
- Reverse the whole line.
- Re-reverse each Latin/number run, so embedded English words and numbers stay readable left-to-right.
// input (visual order)
"World 123 םולש"
// output (logical order)
"שלום World 123"