Home  >  Article  >  Backend Development  >  关于preg_replace非常不解的事情?

关于preg_replace非常不解的事情?

WBOY
WBOYOriginal
2016-06-23 14:01:14768browse

$thumbMark = '200x200';$thumb =$r['thumb'];echo $r['thumb'].'<br />'; // http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg                //$thumb='http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg';$r['thumb'] = preg_replace('/_\d+x\d+/', '_' . $thumbMark,$thumb);echo $r['thumb'].'<br />'; // http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg


为何上述的替换函数无效,而如下给“$thumb”直接赋值,却能正常替换?

$thumbMark = '200x200';$thumb =$r['thumb'];echo $r['thumb'].'<br />'; // http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg$thumb='http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg';$r['thumb'] = preg_replace('/_\d+x\d+/', '_' . $thumbMark,$thumb);echo $r['thumb'].'<br />'; // http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_200x200.jpg




回复讨论(解决方案)

var_dump($r['thumb']);
贴出结果

var_dump($r['thumb']);
贴出结果


string(85) "http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg"

块代码如:

 while ($r = $db->fetch_array($result)) {
            $r['adddate'] = timetodate($r['addtime'], 5);
            $r['editdate'] = timetodate($r['edittime'], 5);

            if ($lazy && isset($r['thumb']) && $r['thumb']) {
                $thumbMark = '100x100';
                if ($list == 1) {
                    $thumbMark = '200x200';
                }
                $thumb =$r['thumb'];
                var_dump($r['thumb']);
                //echo $r['thumb'].'
'; // http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg
                //$thumb='http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg';
                $r['thumb'] = preg_replace('/_\d+x\d+/', '_' . $thumbMark,$thumb);
                //echo $r['thumb'].'
';
                
                
            }

            …………
        }

检查 $list 的值

检查 $list 的值

if ($list == 1) {
$thumbMark = '200x200';
echo 'list:'.$list; // list:1
}

正常

没想到刚接触PHP就碰到怪问题了,难道是环境的问题?

发现一点端倪了,将正则“/_\d+x\d+/”中间的x去除之后,能正常替换,输出“http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_200x200x100.jpg”;

真是太奇怪了,这个“x”到底招谁惹谁了?

发现一点端倪了,将正则“/_\d+x\d+/”中间的x去除之后,能正常替换,输出“http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_200x200x100.jpg”;

真是太奇怪了,这个“x”到底招谁惹谁了?
那说明你的正则是有问题的

那为何直接赋值,如:
$thumb='http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg';

就没有问题。

从你给的东西来看看不出什么错误来 你在仔细调试下


var_dump($r['thumb']);
贴出结果


string(85) "http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg"
你这个字串只有76个字符,为何是string(85) ?

正则这种东西 搞不定的话 只能从源头开始了
$str = '_100x100.jpg';
echo preg_replace('/_/', '', $str);
echo preg_replace('/_\d+/', '', $str);
echo preg_replace('/_\d+x/', '', $str);
echo preg_replace('/_\d+x\d+/', '', $str);
看看哪个有问题 看懂的话就去试试吧...



var_dump($r['thumb']);
贴出结果


string(85) "http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg"
你这个字串只有76个字符,为何是string(85) ?

这个真的很奇怪,最后用正则“/_\d+.{6}\d+/” 解决了。可能在php的函数中,“x”被生成的6个,不知名的东西。85-76=9,应该是9个吧,最后三个可能是数字。

已确定“x”被转换成其他的9个字符,可能是编码不同引起的

已确定“x”被转换成其他的9个字符,可能是编码不同引起的

更正一下,“x”被生成的6个是正确的,

string(85) "http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg",对于后面的字符人为的删除了3个

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