Home > Backend Development > PHP Tutorial > 替换preg_replace.如何老是【吃掉】前面的一个字符.

替换preg_replace.如何老是【吃掉】前面的一个字符.

WBOY
Release: 2016-06-13 10:31:58
Original
948 people have browsed it

替换preg_replace.怎么老是【吃掉】前面的一个字符...
为什么用下面代码进行替换.怎么老是吃掉前面的一个字符......

代码如下:这两句代码都有这个问题...

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->        $newstext = preg_replace('/'.$r[keyname].'/u','<a href="%5C''.%24r%5Bkeyurl%5D.'%5C'" target="\'_blank\'">'.$r[keyname].'</a>',$newstext,$public_r[repkeynum]);
Copy after login


PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->        $newstext = preg_replace('/[^=>]'.$r[keyname].'/u','<a href="%5C''.%24r%5Bkeyurl%5D.'%5C'" target="\'_blank\'">'.$r[keyname].'</a>',$newstext,$public_r[repkeynum]);
Copy after login


比如说...string = "我的美好一天从此开始了"
关键词是 : 美好

替换后就成了 : 我美好一天从此开始了

搞的我好郁闷啊....

------解决方案--------------------
不会出现你说的情况。你的 $r[keyname] 是什么东西?
------解决方案--------------------
第一式正确
第二式是你说的现象: [^=>] 会吃掉一个字符
------解决方案--------------------
求解
------解决方案--------------------

$str = "我的美好的一天开始了";
$newstext = preg_replace('/美好/', '美好', $str);
echo $newstext;
?>
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