Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di mexican
    Registrato dal
    Oct 2001
    residenza
    cava de tirreni
    Messaggi
    3,541

    Spezzare il testo senza spezzare le parole

    Ciao a tutti,
    io ho una query dove recupero un testo, ora io vorrei far vedere solo i primi 300 caratteri però senza spezzare le parole.

    Ovvero se l'ultima frase è:

    sono a casa con nicola, e "cola" sono i caratteri da 301 a 304 vorrei che mi si spezzasse la frase a "con" e quindi mi compaia: "sono a casa con"

    Come posso fare?

  2. #2
    Ciao,
    prova con questa funzione...

    Gli passi il testo ed il numero massimo di caratteri...

    codice:
     public static string ShowPreviewText(string text, int wordCount)
          {
             bool inTag = false;
             int cntr = 0;
             int cntrWords = 0;
    
             // loop through html, counting only viewable content 
             foreach (Char c in text)
             {
                if (cntrWords == wordCount)
                   break;
    
                cntr++;
                if (c == '<')
                {
                   inTag = true;
                   continue;
                }
    
                if (c == '>')
                {
                   inTag = false;
                   continue;
                }
                if (!inTag)
                {
                   cntrWords++;
                   // do not count double spaces, and a space not in a tag counts as a word 
                   //if (c == 32 || lastc != 32)
                   // cntrWords++;
                }
    
    
             }
    
             string substr = text.Substring(0, cntr) + " ...";
    
             //search for nonclosed tags         
             MatchCollection openedTags = new Regex("<[^/](.|\n)*?>").Matches(substr);
             MatchCollection closedTags = new Regex("<[/](.|\n)*?>").Matches(substr);
    
             // create stack           
             Stack<string> opentagsStack = new Stack<string>();
             Stack<string> closedtagsStack = new Stack<string>();
    
    
             foreach (Match tag in openedTags)
             {
                string openedtag = tag.Value.Substring(1, tag.Value.Length - 2);
                // strip any attributes, sure we can use regex for this! 
                if (openedtag.IndexOf(" ") >= 0)
                {
                   openedtag = openedtag.Substring(0, openedtag.IndexOf(" "));
                }
    
                // ignore brs as self-closed 
                if (openedtag.Trim() != "br")
                {
                   opentagsStack.Push(openedtag);
                }
             }
    
             foreach (Match tag in closedTags)
             {
                string closedtag = tag.Value.Substring(2, tag.Value.Length - 3);
                closedtagsStack.Push(closedtag);
             }
    
             if (closedtagsStack.Count < opentagsStack.Count)
             {
                while (opentagsStack.Count > 0)
                {
                   string tagstr = opentagsStack.Pop();
    
                   if (closedtagsStack.Count == 0 || tagstr != closedtagsStack.Peek())
                   {
                      substr += "</" + tagstr + ">";
                   }
                   else
                   {
                      closedtagsStack.Pop();
                   }
                }
             }
    
             return substr;
          }
    Massimo Missaglia
    --------------------------------
    http://www.massimomissaglia.com

  3. #3
    Utente di HTML.it L'avatar di mexican
    Registrato dal
    Oct 2001
    residenza
    cava de tirreni
    Messaggi
    3,541
    Ciao, proprio 10 minuti fa ho trovato una soluzione che credo sia perfetta e molto semplice:

    codice:
    Private r As New Regex("(?s)\b.{1,200}\b", RegexOptions.Compiled)      
    
    Function splitText(ByVal fullText As String) As String         
    Dim m As String = r.Match(fullText).Value         
    
    If m.Length < fullText.Length Then             
    m += "..."        
    End If         
    Return m     
    End Function
    
    Si richiama:
    
    <%=splitText(Testo)%>

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.