preg_replace degree和phi符号

时间:2013-08-14 21:50:33

标签: php regex

我已经尝试过很多方法来取代只有度和(希腊语)的Phi符号。我用Google搜索并完成了文章所说的所有内容,这让我发疯了。我不能使用htmlentities(),因为我不想编码html标签...只是那两个字符,我这样做...它不会返回错误,只是用什么都不替换整个字符串

preg_replace(array('/( )+/', '/\x{00B0}/u', '/\x{03A6}/u', '/\x{03D5}/u'), array(' ','°','Φ','Φ'), $str);

顺便说一下,第一个替换只是摆脱了额外的空白区域并且工作得非常好,但是当我添加其他三个(度数,大写Phi和小写Phi)时,整个字符串被替换为空格。

1 个答案:

答案 0 :(得分:2)

您的代码似乎正在按预期工作

Live Demo

实施例

<强>代码

<?php

$string = 'hi ϕllip       it is 900°';

$output = preg_replace(array('/( )+/', '/\x{00B0}/u', '/\x{03A6}/u', '/\x{03D5}/u'), array(' ','&deg;','&Phi;','&Phi;'), $string );


    echo "this is the output:"  . $output;

<强>输出

this is the output:hi &Phi;llip it is 900&deg;