2011-02-04 3 views
0

мне нужно подсчитать количество слов в пределах диапазона тега ... Текст, как правило, похожи наподсчета слов в тег диапазона

This<span class="highlight_word"> function is useful to highlight words from a simple non-html </span>text.The<span class="highlight_word"><span class="highlight_word"><span class="highlight_word"> regular expression, as a pattern, can match all kinds of text strings helping your application </span></span>validate, compare, compute, decide etc\. </span>With<span class="highlight_word"><span class="highlight_word"><span class="highlight_word"> the temperature up on keywords and searches, many sites have opted </span></span>for highlighting the keywords from their searches\. This can be useful for quickly finding relavant words withing large pages of text\. </span> 

Может кто-нибудь наставит меня о том, как подсчитать количество слов в теги span.

Благодаря

+0

Как вы хотите, граф вернулся? Один счетчик (т. Е. Слова xyz между суммами тегов span), количество в наборе тегов span? Как вы хотите, чтобы вложенные теги обрабатывались? ' слов больше слов больше' Что вы ожидаете от этого примера? – Leigh

ответ

1

На стороне сервера, следующий отсчитывают слова в пролетных тегах в одном счетчике, но вы можете сделать это отдельно для всех элементов пролетных.

$text = 'This<span class="highlight_word"> function is useful to highlight words from a simple non-html </span>text.The<span class="highlight_word"><span class="highlight_word"><span class="highlight_word"> regular expression, as a pattern, can match all kinds of text strings helping your application </span></span>validate, compare, compute, decide etc\. </span>With<span class="highlight_word"><span class="highlight_word"><span class="highlight_word"> the temperature up on keywords and searches, many sites have opted </span></span>for highlighting the keywords from their searches\. This can be useful for quickly finding relavant words withing large pages of text\. </span>'; 
$words = 0; 
preg_match_all("'span[^>]*[>]([^<]+?)[<]/span'is",$text,$matches); 
foreach ($matches[1] as $v) 
{ 
    $words += count(explode(" ",trim($v))); 
} 

UPDATE: я corrcted регулярное выражение немного

+0

Спасибо, работает потрясающе. – shazia

0

можно выполнить, используя следующие две функции

function getTextBetweenTags($string, $tagname) 
{ 
    $pattern = "/<$tagname>(.*?)<\/$tagname>/"; 
    preg_match($pattern, $string, $matches); 
    return $matches[1]; 
} 


function count_words($str) 
{ 
$no = count(explode(" ",$str)); 
return $no; 
} 
+0

Как насчет встроенной функции 'str_word_count()' для подсчета слов вместо вашего пользовательского? – Leigh

+0

необходимо для серверной части – shazia

Смежные вопросы