i know it's not quite what you wanted as we all know i don't code php/jscript
but this is my rendition in python and why i love it,
code:
def reverse_rtl(x):
beta = <rtl characters including space>
count = 0
y = []
zz = []
s = ""
a = ""
b = ""
for letter in x:
#print count
if letter in beta:
y.append(letter)
else:
zz.append(letter)
count += 1
y = y[::-1]
for item in y:
s = s + item
for item in zz:
a = a + item
if zz[-1] != " " and zz[-1] == item:
a = a + " "
b = a + s
return b
u just need some one to translate it
[edit: just added an if statement and fixed it to return the whole string instead of just the rtl]
bug: it does strip the spaces off ltr text with more than 1 space, but rtl text is fine