2011-02-04 7 views
0

que je dois compter le nombre de mots dans la balise span ... Le texte est généralement similaire àmots de comptage dans une balise span

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> 

Quelqu'un peut-il me guider dans la façon de compter le nombre de mots dans les les balises span.

Merci

+0

Comment voulez-vous que le compte est revenu? Un seul compte (c'est-à-dire des mots xyz sont compris entre des balises d'espacement au total), un compte par jeu d'étiquettes d'étendue? Comment voulez-vous que les étiquettes imbriquées soient traitées? ' mots plus de mots plus' Qu'attendez-vous pour cet exemple? – Leigh

Répondre

1

côté serveur, le compte suivant les mots les balises span dans un compteur, mais vous pouvez le faire séparément pour tous les éléments de portée.

$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))); 
} 

MISE À JOUR: i corrcted regexp un peu

+0

Merci travaille étonnamment. – shazia

0

peut être fait en utilisant deux fonctions suivantes

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

Qu'en est-il de la fonction intégrée 'str_word_count()' pour compter les mots au lieu de ceux personnalisés? – Leigh

+0

besoin pour le côté serveur – shazia

Questions connexes