Ver Mensaje Individual
  #49 (permalink)  
Antiguo 08/12/2002, 21:12
Avatar de Kaopectate
Kaopectate
Colaborador
 
Fecha de Ingreso: diciembre-2001
Ubicación: Curaçao (Antillas Holandesas)
Mensajes: 3.179
Antigüedad: 22 años, 4 meses
Puntos: 38
49.- Contar palabras

P: ¿Como puedo contar el número de palabras dentro de un textarea?

R: [ver ejemplo]

Código PHP:
<html>
 <
head>
  <
script language="JavaScript">
   function 
calcula(){
    var 
sTxt document.frm.txt.value;
    var 
sTx2 "";
    var 
sSep document.frm.sep.value;
    var 
iRes 0;
    var 
bPalabra false;
    for (var 
0sTxt.lengthj++){
     if (
sSep.indexOf(sTxt.charAt(j)) != -1){
      if (
bPalabrasTx2 += " ";
      
bPalabra false;
     } else {
      
bPalabra true;
      
sTx2 += sTxt.charAt(j);
     }
    }
    if (
sTx2.charAt(sTx2.length 1) != " "sTx2 += " ";
    for (var 
0sTx2.lengthj++)
     if (
sTx2.charAt(j) == " "iRes++;
    if (
sTx2.length == 1iRes 0;
    
alert("Número de palabras: " String(iRes));
   }
  
</script>
 </head>
 <body>
  <form name="frm">
   Texto:<br>
   <textarea name="txt" rows="5" cols="80"></textarea><br><br>
   Separadores:
   <input type="text" name="sep" value=" ,.;:"><br><br>
   <input type="button" name="btn" value="Calcular" onclick="calcula()">
  </form>
 </body>
</html> 

Última edición por Kaopectate; 24/12/2002 a las 09:58