Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

regex - unicode preg_replace problem in php

I've got the string

$result = "bei einer Temperatur, die etwa 20 bis 60°C unterhalb des Schmelzpunktes der kristallinen Modifikation"

which comes straight from a MySQL table. The table, and the php headers are both set to UTF-8

I want to strip the 'degree' symbol: http://en.wikipedia.org/wiki/Degree_symbol and replace it with the word 'degrees' to get:

"bei einer Temperatur, die etwa 20 bis 60degreesC unterhalb des Schmelzpunktes der kristallinen Modifikation"

but I can't get it to work with preg_replace.

If I do:

$result = preg_replace('/xB0/u'," degrees ", $result ); - I get an empty string

And if I do::

$result = preg_replace('/u00B0/u'," degrees ", $result ); - I get the error:

Warning: preg_replace() [function.preg-replace]: Compilation failed: PCRE does not support L, l, N, U, or u at offset 1 in /var/www/html/includes/classes/redeyeTable.inc.php on line 75

I'm not great with encodings... what am I doing wrong here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use

$result = preg_replace('/x{00B0}/u'," degrees ", $result );

Please see here for more information on the x{FFFF}-syntax.

It's important to note the difference between xB0 and x{00B0}:

  • xB0 denotes a single character with hex-code B0 (176 decimal) which is the degree symbol (°) in ISO-8859-1 for example
  • x{00B0} denotes the unicode codepoint U+00B0 which describes the degree symbol (°) in the unicode system. This codepoint will be encoded using two bytes xC2xB0 when using UTF-8 encoding.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...