問題描述
我正在嘗試用另一個(gè)字符替換字符串中的某個(gè)字符.它們是相當(dāng)晦澀的拉丁字符.我想用 4d9 替換字符(十六進(jìn)制)259,所以我嘗試了這個(gè):
I am trying to replace a certain character in a string with another. They are quite obscure latin characters. I want to replace character (hex) 259 with 4d9, so I tried this:
str_replace("x02x59","x04xd9",$string);
這沒有用.我該怎么做?
This didn't work. How do I go about this?
**附加信息.
謝謝 bobince,這已經(jīng)成功了.雖然,我也想替換大寫的 schwa,但由于某種原因它不起作用.我將 U+018F (?) 計(jì)算為 UTF-8 0xC68F,這將替換為 U+04D8 (0xD398):
Thanks bobince, that has done the trick. Although, I want to replace the uppercase schwa also and it is not working for some reason. I calculated U+018F (?) as UTF-8 0xC68F and this is to be replaced with U+04D8 (0xD398):
$string = str_replace("xC9x99", "xD3x99", $_POST['string_with_schwa']); //lc 259->4d9
$string = str_replace( "xC68F", "xD3x98" , $string); //uc 18f->4d8
我正在將?"復(fù)制到文本框中并發(fā)布.第一個(gè) str_replace
在小寫上工作正常,但在第二個(gè) str_replace
中沒有檢測(cè)到大寫,奇怪.它仍然是 U+018F.我猜我可以通過 strtolower
運(yùn)行字符串,但這應(yīng)該可以工作.
I am copying the '?' into a textbox and posting it. The first str_replace
works fine on the lowercase, but does not detect the uppercase in the second str_replace
, strange. It remains as U+018F. Guess I could run the string through strtolower
but this should work though.
推薦答案
U+0259 拉丁小寫字母 Schwa 在 UTF-16BE 編碼中僅編碼為字節(jié)序列 0x02,0x59.您不太可能使用 UTF-16BE 編碼的字節(jié)字符串,因?yàn)樗皇且环N ASCII 兼容的編碼,而且?guī)缀鯖]有人使用它.
U+0259 Latin Small Letter Schwa is only encoded as the byte sequence 0x02,0x59 in the UTF-16BE encoding. It is very unlikely you will be working with byte strings in the UTF-16BE encoding as it's not an ASCII-compatible encoding and almost no-one uses it.
您想要使用的編碼(唯一支持拉丁語(yǔ) Schwa 和 Cyrillic Sc??hwa 的 ASCII 超集編碼,因?yàn)樗С炙?Unicode 字符)是 UTF-8.確保您的輸入是 UTF-8 格式(如果它來自表單數(shù)據(jù),則將包含表單的頁(yè)面作為 UTF-8 提供).然后,在 UTF-8 中,字符 U+0259 使用字節(jié)序列 0xC9,0x99 表示.
The encoding you want to be working with (the only ASCII-superset encoding to support both Latin Schwa and Cyrillic Schwa, as it supports all Unicode characters) is UTF-8. Ensure your input is in UTF-8 format (if it is coming from form data, serve the page containing the form as UTF-8). Then, in UTF-8, the character U+0259 is represented using the byte sequence 0xC9,0x99.
str_replace("xC9x99", "xD3x99", $string);
如果您確保在文本編輯器中將 .php
文件保存為 UTF-8-no-BOM,則可以跳過轉(zhuǎn)義直接說:
If you make sure to save your .php
file as UTF-8-no-BOM in the text editor, you can skip the escaping and just directly say:
str_replace('?', '?', $string);
這篇關(guān)于替換 unicode 字符的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!