PHP makes good use of regular expressions to process required values ​​​​in strings_PHP tutorial

WBOY
Release: 2016-07-13 17:52:02
Original
914 people have browsed it

When I was collecting pictures from Mo’s website today, I ended up saying that the picture resolution was changed to 320*480, and there was information about pixels in the original picture.

Such as: Clear House (320*480) wallpaper

So the last thing I want to leave is the “Clear House Wallpaper”, what should I do?

First of all, we should immediately think of searching for the string first and then removing it.

Well, first of all, I thought the same way. I defined an array and stored several strings in it, such as (320*480), (480*640), etc., but later I found that there were other types of strings in it, such as ( 320*234) and so on, then it is not impossible for me to list them all, it is just a matter of time. But if we don’t do this, it would be too boring. If you do it this way, half a day will probably pass, and you will be very unhappy.

So what should we do?

First of all, observing multiple data, we found that there should be "*" in it. This must be known, otherwise the values ​​that are not going to be included will increase the workload of the computer.

Then here comes one:

[php]
if(strchr($title, "*"))
                                                                      Used to determine whether a string contains "*";
If it is included, then there must be something we want to remove. What should we do next? Find the string we want to remove and replace it with NULL? Obviously you can't find it in the way above.

And I used string cutting:

[php]

$arr=split('([(]*)[0-9]+*[0-9]+([)]*)',$title);//*must be escaped
$title = $arr[0].$arr[1];
Among them ([(]*)[0-9]+X[0-9]+([)]*) is a regular expression,
([(]*) indicates that there may be (

[0-9]+ means there is at least one number from 0 to 9

[php]

*Exists"*"
Then the above (320*480) is easy to find.
Here is the test program

[php]



$title="Clear House (320*480) Wallpaper";

if(strchr($title, "*"))
                                                                                $arr=split('([(]*)[0-9]+*[0-9]+([)]*)',$title);//*must be escaped
$title = $arr[0].$arr[1];
echo $title;
}  
?>

Author: wolinxuebin


http://www.bkjia.com/PHPjc/478140.html

truehttp: //www.bkjia.com/PHPjc/478140.htmlTechArticleWhen I was collecting pictures from the Mo website today, because I would eventually change the picture resolution to 320*480, There is information about pixels in the original image. Such as: Clear House (320...
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