Home > Backend Development > PHP Tutorial > 替换值为变量该怎么处理

替换值为变量该怎么处理

WBOY
Release: 2016-06-13 13:38:41
Original
818 people have browsed it

替换值为变量该怎么办 ?
我的问题是这样的,$text中大部分都是正常的中文,但个别地方含有unicode字符(类似北这样的字符),我想把它替换掉。于是我找到一段unicode字符转正常汉字的代码,如下:

function unicode() {  

$tmp = file("unicode.txt");  

foreach($tmp
 as
 $key => $val) {  

 $tm1=explode(":",$val);  
 
$char[trim($tm1[1])]=trim($tm1[0]);  
  unset($tm1,$val);  
 }  
 return $char;  
 }  

 

然后准备全文匹配替换了,但是该怎么替换呢?

 
$text =preg_replace($pat,$rec,$text);
 
替换规则应该是:
$pat = "/\d{5}/U";
 
问题是替换值$rec该怎么定义?$rec
应该是根据规则找到的unicode字符,交给unicode() 处理后得到的中文字符。
 
怎么样写才能正确运行替换呢?求高人指教。
 


------解决方案--------------------
呵呵,参数忘记了,,这样子哦

PHP code

$str    = "地方有大方正这样的unicode字";
echo    preg_replace("/(\d{5});?/e", "mb_convert_encoding('\\1;', 'GBK', 'HTML-ENTITIES')", $str);
//地方有大方正这样的unicode字 <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template