[Soci SLIP] spezzare una stringa con intelligenza (PHP)

Roberto -MadBob- Guido bob4mail a gmail.com
Dom 20 Gen 2008 00:17:30 CET


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Davide Corio wrote:
> E' una porcheria abbozzata...ma vista l'ora tarda... :)
>
Mi permetto di riproporne una versione un poco piu' ottimizzata in
termine di prestazione, avendo una complessita' computazionale ridotta.
Mi fa comunque strano che non esista gia' una funzione (o comunque una
sequenza di funzioni) PHP di trattamento stringhe che non risolvano il
problema in un modo quantomeno piu' elegante... Mi appello a chi conosce
il linguaggio meglio di me (che sono un accanito developer C ;-P ).

###################################################################
<?php

function break_string ( $stringa, $index ) {
	if ( $stringa [ $index + 1 ] == " " )
		return substr ( $stringa, 0, $index + 1 ) . "<br/>";

	else {
		$index_left = $index_right = $index;
		$len = strlen ( $stringa ) - 1;

		while ( 1 ) {
			$index_left = $index_left - 1;
			$index_right = $index_right + 1;

			if ( $stringa [ $index_left ] == " " ) {
				$index = $index_left;
				break;
			}

			if ( $stringa [ $index_right ] == " " ) {
				$index = $index_right;
				break;
			}

			/*
				Per evitare di andare a testare
				indici invalidi nella stringa...
			*/
			if ( $index_left == 0 )
				$index = $index_right;
			else if ( $index_right == $len )
				$index = $index_left;
		}

		return substr ( $stringa, 0, $index );
	}
}

?>

###################################################################

- --
+--- -- -                                                 --  ----+
|   Roberto -MadBob- Guido ---+--- bob4mail[AT]gmail.com          |
|                             +--- madbob[AT]jabber.linux.it      .
.                             |
    Step #1 in programming:   +--- http://madbob.homelinux.com    |
|      understand people      +--- http://lobotomy.sf.net         |
|                             +--- http://barberaware.org         |
+--- ---- -                                              -- -  ---+
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFHkoUK3wUck/os3BkRAvtuAJ90DMEkKho71RtDwj+hLmiy0Fti2QCgjZRm
gHLv9ynNGCZMzUdvthMRppo=
=dswR
-----END PGP SIGNATURE-----



Maggiori informazioni sulla lista Soci