简体   繁体   English

删除与字符串上的模式匹配的单词

[英]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.你知道如何得到这个结果吗,也许 php.ini 中有一些函数。 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)?此代码对我有用,是否可以限制此代码的 foreach 数量(与性能有关)?

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')); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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