Home >Backend Development >PHP Tutorial >PHP makes good use of regular expressions to process required values in strings_PHP tutorial
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.
[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 (
[php]
*Exists"*"
Then the above (320*480) is easy to find.
Here is the test program
$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: wolinxuebinhttp://www.bkjia.com/PHPjc/478140.html