What about preg_replace?

Source: Internet
Author: User
What about preg_replace?
$thumbMark = '200x200';$thumb =$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'].'
'; // http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg


Why is the above replacement function invalid? but can it be replaced normally if "$ thumb" is assigned directly as follows?

$thumbMark = '200x200';$thumb =$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'].'
'; // http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_200x200.jpg




Reply to discussion (solution)

Var_dump ($ r ['thumb']);
Post result

Var_dump ($ r ['thumb']);
Post result


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

The block code is as follows:

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: // response ';
$ R ['thumb'] = preg_replace ('/_ \ d + x \ d +/', '_'. $ thumbMark, $ thumb );
// Echo $ r ['thumb'].'
';


}

............
}

Check the value of $ list

Check the value of $ list

If ($ list = 1 ){
$ ThumbMark = '200x200 ';
Echo 'list: '. $ list; // list: 1
}

Normal

I didn't expect a strange problem when I first came into contact with PHP. is it an environment problem?

After removing the x in the middle of the regular "/_ \ d + x \ d +/", the system can replace the regular expression and output "begin ";

It's really strange. Who did this "x" trick provoke?

After removing the x in the middle of the regular "/_ \ d + x \ d +/", the system can replace the regular expression and output "begin ";

It's really strange. Who did this "x" trick provoke?
It indicates that your regular expression is faulty.

Then why do values be assigned directly, such:
$ Thumb = 'http: // response ';

No problem.

From what you gave, you can't see any errors. you are debugging them carefully.


Var_dump ($ r ['thumb']);
Post result


String (85) "http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg"
Your string contains only 76 characters. why is it string (85 )?

Something like regular expressions can only start from the source.
$ 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 );
If you have any questions, please try again...



Var_dump ($ r ['thumb']);
Post result


String (85) "http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg"
Your string contains only 76 characters. why is it string (85 )?

This is really strange, and finally solved with the regular "/_ \ d +. {6} \ d +. In php functions, "x" may generate six unknown items. 85-76 = 9. it should be 9. the last three may be numbers.

It has been determined that "x" is converted to another 9 characters, which may be caused by different encodings.

It has been determined that "x" is converted to another 9 characters, which may be caused by different encodings.

Correct that the six "x" are generated correctly,

String (85) "http://img2.com.cn/images/2014/038/422/14058002224830.9000001763_100x100.jpg", 3 characters are manually deleted

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.