WRInaute occasionnel
Salut, je partage avec vous une petite fonction qui prend en charge l'intégralité des caractères ASCII et les convertis en un texte sans accent, en supprimant les mots d'une taille inférieure à $TAILLE_MINI, les mots d'une taille supérieure à $TAILLE_MAXI, en tronquant le texte à $NOMBRE_MOTS mots et en prenant pour séparateur $SEPARATEUR.
PS: même le signe € est transformé en euro
Voici le code :
Pour l'utiliser :
PS: même le signe € est transformé en euro
Voici le code :
PHP:
<span class="syntaxdefault">function texte_pour_url</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$a</span><span class="syntaxkeyword">)<br />{<br /></span><span class="syntaxdefault">$TAILLE_MINI</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">2</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">$TAILLE_MAXI</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">26</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">$NOMBRE_MOTS</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">15</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">$SEPARATEUR</span><span class="syntaxkeyword">=</span><span class="syntaxstring">'-'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">$c</span><span class="syntaxkeyword">=</span><span class="syntaxstring">'AAAAAAAAAEEAAEAAAAAAAAAAAAAAAAAAEEEEEEEEEEEEEEEE0123456789EEEEEEEabcdefghijklmnopqrstuvwxyzEEEEEEabcdefghijklmnopqrstuvwxyzEEEEEBEEEEEEEEEsECEEEEEEEEEEEEEsECEzyEiEEEEEEEEEEEEEEEE23EEEEEEEEEEEEaaaaaaDceeeeiiiidnoooooxouuuuypEaaaaaaDceeeeiiiionoooooEouuuuyEyE'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault">$d</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">strlen</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$a</span><span class="syntaxkeyword">);</span><span class="syntaxdefault">for </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$i </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> 0</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> $i </span><span class="syntaxkeyword"><</span><span class="syntaxdefault"> $d</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> $i</span><span class="syntaxkeyword">++)</span><span class="syntaxdefault"> $e</span><span class="syntaxkeyword">.=</span><span class="syntaxdefault">$c</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">ord</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$a</span><span class="syntaxkeyword">[</span><span class="syntaxdefault">$i</span><span class="syntaxkeyword">])];</span><span class="syntaxdefault">$e</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">str_replace</span><span class="syntaxkeyword">(array(</span><span class="syntaxstring">'A'</span><span class="syntaxkeyword">,</span><span class="syntaxstring">'B'</span><span class="syntaxkeyword">,</span><span class="syntaxstring">'C'</span><span class="syntaxkeyword">,</span><span class="syntaxstring">'D'</span><span class="syntaxkeyword">,</span><span class="syntaxstring">'E'</span><span class="syntaxkeyword">),array(</span><span class="syntaxstring">''</span><span class="syntaxkeyword">,</span><span class="syntaxstring">' euro '</span><span class="syntaxkeyword">,</span><span class="syntaxstring">'oe'</span><span class="syntaxkeyword">,</span><span class="syntaxstring">'ae'</span><span class="syntaxkeyword">,</span><span class="syntaxstring">' '</span><span class="syntaxkeyword">),</span><span class="syntaxdefault">$e</span><span class="syntaxkeyword">);</span><span class="syntaxdefault">$e</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">preg_replace</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'#\s+#'</span><span class="syntaxkeyword">,</span><span class="syntaxstring">' '</span><span class="syntaxkeyword">,</span><span class="syntaxstring">" $e "</span><span class="syntaxkeyword">);</span><span class="syntaxdefault">$e</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">preg_replace</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'#\s[a-z]{1,'</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$TAILLE_MINI</span><span class="syntaxkeyword">.</span><span class="syntaxstring">'}\s([a-z]{1,'</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$TAILLE_MINI</span><span class="syntaxkeyword">.</span><span class="syntaxstring">'}\s)*#'</span><span class="syntaxkeyword">,</span><span class="syntaxstring">' '</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$e</span><span class="syntaxkeyword">);</span><span class="syntaxdefault">$e</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">preg_replace</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'#\s[a-z0-9]{'</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$TAILLE_MAXI</span><span class="syntaxkeyword">.</span><span class="syntaxstring">',}\s([a-z0-9]{'</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$TAILLE_MAXI</span><span class="syntaxkeyword">.</span><span class="syntaxstring">',}\s)*#'</span><span class="syntaxkeyword">,</span><span class="syntaxstring">' '</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$e</span><span class="syntaxkeyword">);</span><span class="syntaxdefault">$e</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">str_word_count</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$e</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 1</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'0123456789'</span><span class="syntaxkeyword">);</span><span class="syntaxdefault">return implode</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$SEPARATEUR</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">array_slice</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$e</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">0</span><span class="syntaxkeyword">,</span><span class="syntaxdefault">$NOMBRE_MOTS</span><span class="syntaxkeyword">));}</span><span class="syntaxdefault"> </span>
Pour l'utiliser :
PHP:
<span class="syntaxdefault">echo texte_pour_url</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'La conversion est lancée dès que vous nous soumettez l’adresse de la vidéo, ensuite il vous suffira de télécharger le mp3 que nous aurons créé. A la différence d’autres services, l’intégralité du processus de conversion est effectué au sein de notre infrastructure, vous n’avez qu’à télécharger le fichier audio alors stocké sur nos serveurs.'</span><span class="syntaxkeyword">);<br /><br /></span><span class="syntaxcomment">//retourne : conversion-est-lancee-des-que-vous-nous-soumettez-adresse-video-ensuite-vous-suffira-telecharger-mp3 </span><span class="syntaxdefault"></span>