繁体   English   中英

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

[英]Delete words that match the pattern on the string

我必须根据与模式相似的词来过滤字符串。 我需要删除与公式匹配的单词:

<?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



?>

你知道如何得到这个结果吗,也许 php.ini 中有一些函数。 我试图搜索,不幸的是我没有找到任何信息。 感谢您提供的所有帮助和示例

此代码对我有用,是否可以限制此代码的 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