我用Google搜索了2个多小时关于这个问题我想解决但仍然没有运气......任何人都请帮忙...
我想将所有用户文本输入保存到mysql数据库,在php中我需要将所有非字母数字字符转换为unicode用于中文字符和其他符号。那么preg_replace如何通过当前匹配的值将其传递给转换函数。
实施例: preg_replace(“/ [^ A-Za-z0-9] /”,turn_unicode($ matchedvaluehere),$ string);
答案 0 :(得分:1)
你的意思是这样的: http://php.net/manual/en/function.preg-replace-callback-array.php 要么 http://php.net/manual/en/function.preg-replace-callback.php 在旧版本的PHP中。
<?php
$subject = 'Aaaaaa Bbb';
preg_replace_callback_array(
[
'~[a]+~i' => function ($match) {
echo strlen($match[0]), ' matches for "a" found', PHP_EOL;
},
'~[b]+~i' => function ($match) {
echo strlen($match[0]), ' matches for "b" found', PHP_EOL;
}
],
$subject
);
?>
答案 1 :(得分:0)
也许你可以使用group然后在回调参数中反向引用它。像
这样的东西preg_replace("/([^A-Za-z0-9 ])/", turn_unicode("\$1"), $string);