[Php-it] [PHP]- gestire le stringhe

Alessandro Marinuzzi alecosnet at inwind.it
Fri Mar 30 10:49:52 CEST 2007


fbisignano at silaweb.it@ ha scritto:
> Salve, ho un problema nel gestire alcune stringhe che hanno
> spazi nelle stringhe.
> La mia stringa è del tipo "C i a o   C o m e   s t a i" e
> vorrei togliere tutti gli spazi. Ho pensato di utilizzare le
> espressioni regolari... ma non saprei dove mettere le
> mani... potreste aiutarmi?
>
> grazie
>
> Francesco
>   
Io userei:

<?php
$str = 'C i a o   C o m e   s t a i';
$str = rtrim($str);
if(preg_match('/\s{2,}/', $str)) {
  $str = preg_replace('/\s{2,}/', '·', $str);
}
if(preg_match('/\s{1,1}/', $str)) {
  $str = preg_replace('/\s{1,1}/', '', $str);
}
$str = str_replace('·', ' ', $str);
echo $str;
?>

funziona alla grande ;-) fa esattamente quello che vuoi tu ;-)
Spero di esserti stato utile :-)

-- 
Alessandro Marinuzzi
--------------------
http://www.alecos.it
--------------------



More information about the Php-it mailing list