Php makes good use of regular expressions to deal with the values required in strings

Source: Internet
Author: User
Php makes good use of regular expressions to process the Value & nbsp, in the end, I will change the image resolution to 320*480, and the original image contains pixel information. & Nbsp; e. g.: clear cabin (320*480) Wallpaper & nbsp; in the end, php makes good use of regular expressions to process the values required in strings.

Today, when collecting images from the Momo website, the resolution of the images will be changed to 320*480, and the original images contain pixel information.

For example, clear Cottage (320*480) wallpaper

So what should I do if I want to leave the "clear cottage wallpaper?

First, we should immediately think that we should first find the string and remove it.

Well, first of all, I thought so too. I defined an array and saved several strings, such as (320*480) and (480*640). but later I found that, if there are other types of stream (320*234), it is not impossible for me to list them all, but it is just a matter of time. But we don't want to do this. it's boring. It is estimated that half a day has passed, and it is quite uncomfortable.

So what should we do?

First, observe multiple pieces of data. we found that there should be "*", which must be known. Otherwise, the values that do not have to be included will increase the workload of the computer.

Here comes:

If (strchr ($ title ,"*")){Used to determine whether a string contains "*";

If so, there must be something we want to remove. what should we do next? find the string to be removed and replace it with NULL? Obviously, you cannot find it in the way above.

I used the string cutting method:

$ Arr = split ('([\ (] *) [0-9] + \ * [0-9] + ([\)] *)', $ title ); // * be sure to escape $ title = $ arr [0]. $ arr [1];([\ (] *) [0-9] + X [0-9] + ([\)] *) is a regular expression,

([\ (] *) Indicates that there may be (

[0-9] + indicates that at least one number from 0 to 9 exists.

\ * There is "*", so the above (320*480) is very simple to find out. Here is the test program.

<Pre name = "code" class = "php"> <? Php $ title = "clearhome (320*480) wallpaper"; if (strchr ($ title ,"*")) {$ arr = split ('([\ (] *) [0-9] + \ * [0-9] + ([\)] *)', $ title); // * be sure to escape $ title = $ arr [0]. $ arr [1]; echo $ title ;}?>

 


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.