开发者

preg_replace ('/σ/', 'ς', $cat_name);

how can change only the last letter of any word of a string with regular expressions?

I use mb_strtolower() for change strings from upper to lower in Greek language and I have problem with fin开发者_开发问答al 's'.


Use a word boundary -- \b -- to match the start or end of a word. Try

preg_replace ('/σ\b/', 'ς', $cat_name);


preg_replace (‘/σ\b/’, ‘ς’, $cat_name);


EDIT2:

mb_internal_encoding("UTF-8");
$s = "sdfΣ";
echo $s,"\n";
echo mb_ereg_replace("Σ\\b", 'ς', $s);

EDIT: Apparently, this doesn't work for greek leters, preg_replace will match "f", not the sigma.

Not that I know any greek, but σ is labelled as "Greek small letter sigma", so you ask for the lowercase version, it should still give σ.

mb_strtolower() should work. You're probably doing something wrong. Post the relevant portion of your program.

Anyway, with preg_replace and assuming both your script and $cat_name are encoded in UTF-8, it's:

echo preg_replace ("/\\w\\b/eu", 'mb_strtolower(\'\\0\', \'UTF-8\')', $cat_name);

for instance, if $cat_name == "sdfÉ", this gives sdfé.


The code that I use is: mb_internal_encoding("ISO-8859-7"); $cat_name=mb_strtolower($categories_name); $cat_name=preg_replace('/σ\b/', 'ς', $cat_name);

The problem is that when I use $cat_name=preg_replace('/σ/', 'ς', $cat_name); work right and replace every 'σ' with 'ς'

My server has PHP 5.2.6-1+lenny8 with Suhosin-Patch 0.9.6.2 (cli) (built: Mar 14 2010 09:07:33)

Local in my laptop I use Xamp for windows with PHP Version 5.3.1 and the switch \b work. So the problem is in the different version of Php or in php.ini ?

Thanks for the replies..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜