2012-05-28 1 views
0

이 코드를 살펴보고 (사용자 budwiser 덕분에) 텍스트의 특정 단어를 찾습니다. 광고에 스팬 클래스 :배열에서 작업하기 위해 텍 스에서 특정 단어를 검색하고 스팬 클래스에 광고를 수정하는 코드 수정

function highlightWord($word, $text){ 
    return str_replace($word, '<span class="highlighted">' . $word . '</span>', $text); 
} 

$text = highlightWord('fitness', $text); 

이 코드를 배열에 적용하는 방법은 무엇입니까?

$words = array("fitness", "aerobic", "fashion"); 

$text = highlightWord($words, $text); 

어떤 아이디어 : 나는 이런 식으로 뭔가를해야 할 것

?

감사합니다.

UPDATE

$profile_rule = 'profile LIKE (\'fitness\') AND profile LIKE (\'aerobic\')'; 
preg_match_all('/\'(.*?)\'/',$profile_rule,$match); 

어떤 생각을 왜 $ 일치하는 배열이 작동하지 않는 이유는 무엇입니까? $ profile_rule에서 fitness와 aerobic이라는 단어를 추출하고 위의 함수에 $ match 배열을 사용하고 싶습니다.

타이!

답변

0

트릭을 수행해야합니다. 단순히 단어를 반복하고 강조 표시 코드를 추가하십시오.

function highlightWords(Array $words, $text) 
{ 
    // Loop through array of words 
    foreach($words as $word) 
    { 
     // Highlight word inside original text 
     $text = str_replace($word, '<span class="highlighted">' . $word . '</span>', $text); 
    } 

    // Return modified text 
    return $text; 
} 
+0

와우, 테스트하려면 몇 분만 기다려주세요. A가있는 대문자입니까? – webmasters

+0

기억이 안납니다. 두 가지 방법으로 사용했다고 생각합니다. 어쨌든 힌트를 표시하는 것일뿐입니다. –

+0

Ty 대단히 효과적입니다. 코드를 업데이트하고 있습니다. 어쩌면 도움을받을 수 있습니다. 잠시만 기다려주세요. ty – webmasters

관련 문제