2014-06-30 6 views
0

Je souhaite diviser une phrase en un paragraphe et chaque paragraphe doit contenir moins de nombres de mots. Par exemple:Diviser les phrases en paragraphes en fonction du nombre de mots comptés

Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. 

Paragraph 1: 
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. 

Paragraph 2: 
Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. 

Dans l'exemple ci-dessus, les mots moins de 20 est dans un paragraphe 1 et le repos sont sur le paragraphe 2.

Est-il possible d'y parvenir en utilisant php?

J'ai essayé $abc = explode(' ', $str, 20); qui va stocker 20 mots dans un tableau puis le reste d'entre eux pour le tableau $ abc ['21 ']. Comment puis-je extraire des données du premier tableau 20 comme le premier paragraphe, puis le reste comme le deuxième paragraphe?

+0

Votre dernier paragraphe: 'Je l'ai essayé ...' est complètement faux, s'il vous plaît reformuler. – Athafoud

+0

vous pouvez essayer de convertir votre chaîne dans un tableau, puis stocker les 20 premiers caractères dans une chaîne et le reste dans un autre. – Aradhna

+0

Il suffit d'utiliser 'implode' après' explode'ing votre phrase. http://stackoverflow.com/questions/5956610/how-to-select-first-10-words-of-a-sentence – TribalChief

Répondre

0

Première chaîne divisée en phrases. Puis bouclez le tableau des phrases, commencez par ajouter la phrase à un tableau de paragraphes, puis comptez les mots dans cet élément du tableau de paragraphes, si le compteur de paragraphes est supérieur à 19.

$string = 'Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source.'; 

$sentences = preg_split('/(?<=[.?!;])\s+(?=\p{Lu})/', $string); 

$ii = 0; 
$paragraphs = array(); 
foreach ($sentences as $value) { 
    if (isset($paragraphs[$ii])) { $paragraphs[$ii] .= $value; } 
    else { $paragraphs[$ii] = $value; } 
    if (19 < str_word_count($paragraphs[$ii])) { 
     $ii++; 
    } 
} 
print_r($paragraphs); 

Sortie:

Array 
(
    [0] => Contrary to popular belief, Lorem Ipsum is not simply random text.It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. 
    [1] => Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. 
) 

séparateur de phrase trouvé ici: Splitting paragraphs into sentences with regexp and PHP

Questions connexes