Preg_filter-execute a regular expression to search and replace
Mixed preg_filter (mixed $ pattern, mixed $ replacement, mixed $ subject [, int $ limit =-1 [, int & $ count])
Preg_filter () is equivalent to preg_replace () except that it only returns (possibly converted) results that match the target. For more information about how this function works, see preg_replace ().
The code is as follows: |
Copy code |
<? Php $ Subject = array ('1', 'A', '2', 'B', '3', 'A', 'B', '4 '); $ Pattern = array ('/d/', '/[a-z]/', '/[1a]/'); $ Replace = array ('A: $ 0', 'B: $ 0', 'C: $0 '); Echo "preg_filter returnsn "; Print_r (preg_filter ($ pattern, $ replace, $ subject )); // Only returns the result of regular match Echo "preg_replace returnsn "; Print_r (preg_replace ($ pattern, $ replace, $ subject )); // Return all results ?>
Preg_filter returns Array ( [0] => A: C: 1 [1] => B: C: [2] => A: 2 [3] => B: B [4] => A: 3 [7] => A: 4 ) Preg_replace returns Array ( [0] => A: C: 1 [1] => B: C: [2] => A: 2 [3] => B: B [4] => A: 3 [5] => [6] => B [7] => A: 4 ) |
Preg_replace function
The code is as follows: |
Copy code |
<? Php $ Str = "as2223adfsf0s4df0sdfsdf "; Echo preg_replace ("/0/", "", $ str); // Remove 0 characters, which is equivalent to the replace function, preg_replace ("/0/", "", $ str); this means that 0 is changed to. Echo preg_replace ("/[0-9]/", "", $ str); // remove all numbers Echo preg_replace ("/[a-z]/", "", $ str); // remove all lowercase letters. Echo preg_replace ("/[A-Z]/", "", $ str); // remove all uppercase letters Echo preg_replace ("/[a-z, A-Z]/", "", $ str); // remove all letters $ Str = "as2223adfsAAf0s4df0s Chinese dD Zhongnanhai DDfsdf "; Echo preg_replace ("/[a-z, A-Z, 0-9]/", "", $ str); // remove all letters and numbers ?>
|
After the above examples, I believe everyone knows what the role of [] and above is. You can also see that the matched string must be added // (see the first parameter in the example)
The code is as follows: |
Copy code |
<? Php $ Str = "acsdcs <55555555> SC <6666> sdcd "; Echo preg_replace ("/<. *>/", "", $ str ); |
// This indicates the part starting with <and ending with>. The output result is acsdcssdcd.
Note: the above. * represents any character, that is, no matter what is wrapped in, remove it. Represents any character, * represents any number
Now let's change it. What if we don't want to use any number?
$ Str = "acsdcs <55555555> SC <6666> sdcd ";
Echo preg_replace ("/<. {4}>/"," ", $ str); // output: acsdcs <55555555> scsdcd because {4} specifies the condition: <> The condition is not met because it contains four characters. Therefore, <55555555> The condition is not met and is not replaced.
Note: Now we have learned another knowledge point {number} to specify the number above, * to indicate any number (0 -- unlimited)
In addition to *, {specified number of times}, there are also many expressions:
The code is as follows: |
Copy code |
<? Php $ Str = "acsdcs <55555555> SC <6666> sd <> cd "; Echo preg_replace ("/<[0-9] *>/", "", $ str ); // Output acsdcscd Echo "Echo preg_replace ("/<[0-9] +>/", "", $ str ); // Enter acsdcsscsd <> cd ?> |
As long as the above example is to express the difference between * and +, * indicates that the number of duplicates is 0 or n times, and + indicates more than 1 time, in this example, <[0-9] +> indicates that <> there must be at least one number to meet the condition.
I believe that everyone knows why * in the above example is different from the result output by use +.
Again:
The code is as follows: |
Copy code |
<? Php $ Str = "acsdcs <55555555> SC <6666> sd <> cd "; Echo preg_replace ("/<[0-9]?> /"," ", $ Str ); // Output acsdcs <55555555> SC <6666> sdcd ?> |
Check [0-9]? Here? It indicates that if it is 0 times or 1 time, it exceeds 1 time and does not meet the conditions.
To sum up, we learned * +? And braces {} indicate the number of repetitions.
The code is as follows: |
Copy code |
Bytes ---------------------------------------------------------------------------------------------------- $ S = preg_replace ("/(.*? [Monthly pass | request | more]. *?) /I "," ", $ s ); Preg_match_all ('/href = "([0-9] +). shtm"> (. + ?) </A>/I ', $ s, $ arr_dstorycate ); Print_r ($ arr_dstorycate ); Bytes ---------------------------------------------------------------------------------------------------- Preg_match_all ("/ /I ", $ content, $ arr_dstorycate ); Print_r ($ arr_dstorycate ); |