简体   繁体   中英

Delete words that match the pattern on the string

I have to filter a string according to words similar to the pattern. I need to delete words that match the formula:

<?php
$string = 'armin van burren yummy';
$pattern = 'a%n v%n b%n';

//result: Yummy

$string = 'Beyonce - love on top';
$pattern = 'b%e';

//result: Love on top

$string = 'Ed Sheeran - Shape of You';
$pattern = 'e_ s_____n';

//result: Shape of You



?>

Do you have any idea how to get this result, maybe there is some function in php. I tried to search, unfortunately I didn't find any information. Thank you for all the help and examples

This code works for me, is it possible to limit the amount of foreach for this code (performance is concerned)?

function filterlike($arr, $like){
                if ($arr && $like){
                    $like = preg_replace('/_{1,}/','.*', $like);
                    $like = preg_replace('/%/','.*', $like);
                    $like = explode(' ', $like);
                    $filter = array();
                    foreach ($arr as $a){
                        foreach ($like as $l){
                            $a = preg_replace('/'.$l.'/', '', $a);
                        }
                        $filter[] = $a;
                    }
                    return $filter;
                }
 }

print_r(filterlike(array('armin', 'van', 'burren', 'jummy'), 'a%n v%n b%n'));
print_r(filterlike(array('armin', 'van', 'burren', 'jummy'), 'a___n v_n b____n')); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM