1

I need to display some RTL text inline on a LTR page.

I'm storing country names in a single field in a MySQL database in the format EnglishName (LocalLanguageName). When the local language is RTL text the output created by my PHP is broken.

For example Israel is stored in the database as:

Israel (ישר×ל)

My PHP code is:

print "<li>$row[Country], ";
print date('jS M Y',strtotime($row[StartDate]));
print "</li>\n";

Which outputs:

<li>Israel (ישראל), 1st Jan 2013</li>

If I remove the date line the closing parenthesis and the comma are in the correct position.

If I put "x". in front of the date I get the expected:

<li>Israel (ישראל), x1st Jan 2013</li>

Adding dir="LTR" to the <li> element doesn't work because the output html is incorrect.

How do I get an output of <li>LTR name (RTL name), date</li>?

Thanks

4

2 回答 2

1

The Arabic characters set the parser into RTL mode, punctuation is ignored, letters from the English alphabet trigger the parser back into LTR mode (which is why adding the x character as mentioned in the OP 'fixes' the output), but numerals are still considered to be RTL so the numerals of the date are still considered to be part of the RTL string as is the punctuation ), that gets caught up between them.

You can force the parser back into LTR mode by using the UTF-8 left to right marker:

print "<li>$row[Country]\xe2\x80\x8e, ";

Which results in:

<li>Israel (ישראל)‎, 1st Jan 2013</li>
于 2013-03-29T19:01:00.840 回答
1

It doesn't have a lot to do with PHP, but with HTML and the Unicode bidirectional algorithm. It appears that you always put a number after the country name, and the algorithm does some magic to numbers that are adjacent to words with different directionality. Sometimes this magic is useful and sometimes not.

If your general environment is mostly English, then the simplest thing to do is to add the HTML entity &lrm; after the closing parentheses after the country name. You don't even need a condition and you can just do it after all the country names - it won't hurt after left-to-right country names.

A more elegant way to do it is to put the country name, including the part in the parentheses in the HTML tag <bdi>. It means "bi-directional isolation"; in simple terms, it's like a <span> tag, but it isolates its contents in a way that prevents the bidi magic mentioned in the beginning of the answer. Though it's more elegant, this method has a drawback: it won't work in Microsoft Internet Explorer (saw it coming?). If you don't care about Microsoft Internet Explorer too much, I suggest using <bdi>.

于 2013-03-29T19:01:32.797 回答