繁体   English   中英

使用PHP在关键字数组/字符串中进行关键字匹配

[英]Keyword matching in an array/string of keywords using PHP

我有一段PHP代码,如下所示:

$words = array(
            'Artist' => '1',
            'Tony' => '2',
            'Scarface' => '3',
            'Omar' => '4',
            'Frank' => '5',
            'Torrentino' => '6',
            'Mel Gibson' => '7',
            'Frank Sinatra' => '8',
            'Shakes' => '9',
            'William Shakespeare' => '10'
    );
$text = "William Shakespeare is very famous in the world. An artist is a person engaged in one or more of any of a broad spectrum of activities related to creating art, practicing the arts, and/or demonstrating an art. Artist is a descriptive term applied to a person who engages in an activity deemed to be an art. Frank Sinatra was an American singer, actor, director, film producer, and conductor. Frank Sinatra was born on December 12, 1915, in Hoboken, New Jersey, the only child of Italian immigrants Natalina Garaventa and Antonino Martino Sinatra, and was raised Roman Catholic.";

$re = '/\b(?:' . join('|', array_map(function($keyword) {
    return preg_quote($keyword, '/');
}, array_keys($words))) . ')\b/i';

preg_match_all($re, $text, $matches);
foreach ($matches[0] as $keyword) {
    echo $keyword, " ", $words[$keyword], "\n";
}

该代码返回以下内容:

William Shakespeare 10 artist Artist 1 Frank 5 Frank 5

该代码很好地执行了操作,以免回显莎士比亚中的'Shakes' => '9'类的局部关键字。 但是,正如你所看到的代码无法检测'Frank Sinatra' => '8'为关键字,因为它是在Frank Sinatra was an American singer ,也是artist没有任何值(即1 )。 您能否请我以某种方式更改代码以呼应William Shakespeare 10 artist 1 Artist 1 Frank 5 Frank 5 Frank Sinatra 8 Frank Sinatra 8而不是William Shakespeare 10 artist Artist 1 Frank 5 Frank 5在当前版本中。 谢谢你的帮助。

我设法达到了结果:

威廉·莎士比亚10艺术家1艺术家1 Frank Sinatra 8 Frank Sinatra 8

使用代码:

<?php

mb_internal_encoding('UTF-8');
$words = array(
            'Artist' => '1',
            'Tony' => '2',
            'Scarface' => '3',
            'Omar' => '4',
            'Frank' => '5',
            'Torrentino' => '6',
            'Mel Gibson' => '7',
            'Frank Sinatra' => '8',
            'Shakes' => '9',
            'William Shakespeare' => '10'
    );


uksort($words, function ($a, $b) {
    $as = mb_strlen($a);
    $bs = mb_strlen($b);

    if ($as > $bs) {
        return -1;
    }
    else if ($bs > $as) {
        return 1;
    }
    return 0;


});

$words_ci = array();

foreach ($words as $k => $v) {
    $words_ci[mb_strtolower($k)] = $v;
}

$text = "William Shakespeare is very famous in the world. An artist is a person engaged in one or more of any of a broad spectrum of activities related to creating art, practicing the arts, and/or demonstrating an art. Artist is a descriptive term applied to a person who engages in an activity deemed to be an art. Frank Sinatra was an American singer, actor, director, film producer, and conductor. Frank Sinatra was born on December 12, 1915, in Hoboken, New Jersey, the only child of Italian immigrants Natalina Garaventa and Antonino Martino Sinatra, and was raised Roman Catholic.";

$re = '/\b(?:' . join('|', array_map(function($keyword) {
    return preg_quote($keyword, '/');
}, array_keys($words))) . ')\b/i';



preg_match_all($re, $text, $matches);
foreach ($matches[0] as $keyword) {
    echo $keyword, " ", $words_ci[mb_strtolower($keyword)], "\n";
} 

暂无
暂无

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

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