Home  >  Article  >  Backend Development  >  php preg_replace 乱码有关问题

php preg_replace 乱码有关问题

WBOY
WBOYOriginal
2016-06-13 10:54:361198browse

php preg_replace 乱码问题

还是用代码来说话吧:

?

$pattern = "/ (href|src)=['|\"]http:\/\/(\w*\.?mysite\.com)\/([^\?'\"]*)['|\"]/i";

$repl = " \1='http://\2/\3?p=faskdfkasdkf'";

$result="中国人";
$result = preg_replace($pattern, $repl, $result);
echo $result;

?

上面这段代码的作用,我就不解释了,可以自己执行一下。

?

执行后你会发现出现有乱码,经过查资料发现是$repl写错了,应该用\\来做代替位置,正确格式如下:

?

$repl = " \\1='http://\\2/\\3?p=faskdfkasdkf'";

?

还有一点一定要注意:mb_ereg_replace 在并发情况下会内存泄露,造成压力测试无法通过。

Statement:
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