2014-03-12 1 views
0

개념은 키워드 배열과 기사가 있다는 것입니다. 나는 그 키워드 중 어떤 것이 기사의 집합에 존재 하는지를 알아 내고, 성능과 속도를 염두에 두는 것이 가장 좋은 방법인지 궁금합니다.PHP - 기사에 키워드 집합 찾기

기본적으로 키워드는 3 단어 이상으로 구성되지만 10 단어를 초과하지는 않습니다. 키워드가 기사에 존재하면 기사에있는 키워드 만 반환합니다.

$articles = "Maybe it’s less true than it used to be that people are made of 
     place--that the same elements that form coal and clay and bogs and ice form 
     faces, voices and characters. I wrote my first collection of short stories, 
     The Bostons, in homage to this book, hoping, as did Joyce’s young Stephen 
     Dedalus, to encounter for the millionth time the reality of experience and to 
     forge in the smithy of my soul the uncreated conscience of some island-dwellers 
     I knew." 

키워드 :

$keywords = "less true than, people are made, smithy of my soul, uncreated 
      conscience, this is a test string" 

밖으로 뮤스 넣어 수 :

"less true than, people are made, smithy of my soul, uncreated conscience" 

이미

$articles = mb_split(' +', $articles); 
    foreach ($articles as $key => $word) 
$articles [$key] = trim($word); 

    //Search for keywords  
    $keywords = str_replace(' ', '', $keywords); 
    $keywords = mb_split('[ ,]+', mb_strtolower($keywords, 'utf-8')); 

    $result = implode(',', array_intersect($keywords, $articles); 
,536를 사용하여 프로그램

우리가 기사를 가지고 있다고 가정

하나의 키워드에 대해서만 작동합니다. 여러 키워드를 사용하는 방법을 모르겠습니다.

+0

키워드가 실제로 여러 단어로 구성 될 수 있습니까? 예를 들어 귀하의 예에서 "키워드"가 "보다 적습니다", 맞습니까? – Nico

+0

정규 표현식과 preg_match에 대해 들어 봤습니까? –

답변

0

strpos()이 필요합니다. 작동 -

$res = Array(); 
foreach(explode(", ",$keywords) as $keyword){ 
    if(strpos($articles, $keyword)){ 
     $res[] = $keyword; 
    } 
} 
$matched = implode($res,", "); 
var_dump($matched); 
/** OUTPUT **/ 
string 'less true than, people are made, smithy of my soul, uncreated conscience' (length=72) 
0

Regular Expressions 당신을 도울 수 있습니다. 이 작업은 here에서 볼 수 있습니다. 키워드 문자열에 문제가 있었을 수 있습니까?

$articles = "Maybe it’s less true than it used to be that people are made of 
    place--that the same elements that form coal and clay and bogs and ice form 
    faces, voices and characters. I wrote my first collection of short stories, 
    The Bostons, in homage to this book, hoping, as did Joyce’s young Stephen 
    Dedalus, to encounter for the millionth time the reality of experience and to 
    forge in the smithy of my soul the uncreated conscience of some island-dwellers 
    I knew."; 

$keywords = "less true than, people are made, smithy of my soul, uncreated conscience, this is a test string"; 

$keywordsArray = explode(', ',$keywords); 

$pattern = '/'.implode('|',$keywordsArray).'/'; 
preg_match_all($pattern,$articles,$matches); 

var_dump($matches); 
0
$matches = array_unique(
    preg_match_all(
     '/'.implode('|', explode(', ', $keywords).'/', 
     $articles 
    ) 
); 
0

$ 기사 = "어쩌면 그것은 사람들이 장소 만들어진 것으로 예전보다 적은 사실 - 석탄 및 점토와 습지와 얼음 양식을 얼굴, 목소리 형성 같은 요소 만번 시간의 대장간에서 경험의 현실과 위조가 발생할 조이스의 젊은 스티븐 Dedalus했던 것처럼 문자는. 나는, 희망,이 책에 경의 짧은 이야기의 나의 첫 컬렉션, Bostons를 썼다 내 영혼이 어떤 섬 주민들의 창조되지 않은 양심 나는 알고 있었다. " ;

$ 키워드 = "사람들은 만들어 내지 못합니다, 내 영혼의 대장, 창조되지 않은 양심, 이것은 테스트 문자열입니다";

$ keyword = explode (',', $ keywords);

의 foreach ($ AS $ 키워드 키 => $ 값) {

if(strpos($articles,$value)) { 

     $finalstring .= $value.','; 
} 
} 

에코 $의 finalstring;