Visualizzazione dei risultati da 1 a 7 su 7

Discussione: Limitare testo

  1. #1

    Limitare testo

    Ciao a tutti..


    Codice PHP:
    <?php
    $result 
    mysql_query("SELECT *, date_format(data, '%d/%m') as data_format FROM news ORDER BY id DESC LIMIT 0,20")
    or die(
    "Query non valida: " mysql_error());
    while (
    $row mysql_fetch_array($result))
    { echo 
    "&raquo;$row[data_format][b]<a href=\"news/news.php?id=$row[id]\"title=\"$row[titolo]\"> $row[titolo]</a>[/b]
    $row[testo]<hr>";}
    ?>
    Questo script mi visualizza le news nella home. L'inconveniente è che spesso [testo] è molto lungo e vorrei limitarlo a tot caratteri o parole..

    come posso fare?

    Grazie

  2. #2

  3. #3
    Grazie marketto per la risposta..

    Ho dato un occhiata ai link che mi hai postato ma, essendo un neofita, ho qualche problema nell'integrare quelle funzioni nello script..

    Potresti essere così gentile da spiegarmi più dettagliatamente?

    Grazie

  4. #4
    Moderatore di Server Apache L'avatar di marketto
    Registrato dal
    Sep 2001
    Messaggi
    5,858
    codice:
    function strSelect( $myString, $maxLength ) {
    	// andr3a
    	$out = "";
    	$s = explode( " ",$myString );
    	for( $i = 0, $cs = count( $s ); $i < $cs; $i++ ) {
    		$out .= $s[$i]." ";
    		if( isSet( $s[$i+1] ) && ( strlen( $out ) + strlen( $s[$i+1] ) ) > $maxLength ) {
    			break;
    		}
    	}
    	return rtrim( $out );
    }
    echo strSelect("testo troppo lungo, decisamente troppo lungo",20);
    think simple think ringo

  5. #5
    Nulla da fare..ho provato ma credo di aver scritto una porcheria

    Codice PHP:
    <?php
    $result 
    mysql_query("SELECT *, date_format(data, '%d/%m') as data_format FROM news ORDER BY id DESC LIMIT 0,20")
    or die(
    "Query non valida: " mysql_error());
    function 
    strSelect$myString$maxLength ) {
        
    // andr3a
        
    $out "";
        
    $s explode" ",$myString );
        for( 
    $i 0$cs count$s ); $i $cs$i++ ) {
            
    $out .= $s[$i]." ";
            if( isSet( 
    $s[$i+1] ) && ( strlen$out ) + strlen$s[$i+1] ) ) > $maxLength ) {
                break;
            }
        }
        return 
    rtrim$out );
    }

    while (
    $row mysql_fetch_array($result))
    { echo 
    "&raquo;$row[data_format][b]<a href=\"news/news.php?id=$row[id]\"title=\"$row[titolo]\"> $row[titolo]</a>[/b]
    strSelect(
    $row[testo],20)<hr>";}
    ?>

  6. #6
    codice:
    <?php
    function strSelect( $myString, $maxLength ) {
    	// andr3a
    	$out = "";
    	$s = explode( " ",$myString );
    	for( $i = 0, $cs = count( $s ); $i < $cs; $i++ ) {
    		$out .= $s[$i]." ";
    		if( isSet( $s[$i+1] ) && ( strlen( $out ) + strlen( $s[$i+1] ) ) > $maxLength ) {
    			break;
    		}
    	}
    	return rtrim( $out );
    }
    
    $result = mysql_query("SELECT *, date_format(data, '%d/%m') as data_format FROM news ORDER BY id DESC LIMIT 0,20")
    or die("Query non valida: " . mysql_error());
    
    while ($row = mysql_fetch_array($result)) { 
        echo "&raquo;$row[data_format]<a href=\"news/news.php?id=$row[id]\"title=\"$row[titolo]\"> $row[titolo]</a>
    ".strSelect($row[testo],20)."<hr>";}
    ?>
    E' la mia opinione ed io la condivido
    Non condivido la tua idea ma darei la vita perché tu la possa esprimere (Voltaire)
    Scrivi sul muro

  7. #7
    Grazie mille!!

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.