Home

JiFile for Joomla!

JIFile
JiFile is a component for Joomla! that allows you to index file contents (PDF, DOC, etc..) to perform searches in them.

Learn more...  Demo

JoomPhoto Mobile

JPhotoMobile
JoomPhoto Mobile is a component for Joomla! that allows you to share the photos from your Android device to your site Joomla.

Learn more...  Demo

iFile Framework

IFile
IFile is an open source framework written entirely in PHP, allows the indexing of textual content of a document (DOC, PDF, EXCEL, etc.) and a quick search within them.

Learn more...  Demo

Easy Language

EasyLanguage
Easy Language is a plugin for easy and immediate management of multilingual texts in every possible area of joomla, articles, components, modules, metadata, template, other components(example K2) etc.

Learn more...

Article Book Effect

Article Book Effect
View Joomla articles with the effect turns the page of a book. This plugin will display the contents of an article in Joomla as a real book or magazine, using all the benefits of HTML5

Learn more...  Demo

 

Passport photo

Passport photo
The most popular Android app that allows you to print photos cards for your documents with your Android smartphone, in a simple and intuitive way.

Learn more...

 

Crazy Shadow

Crazy Shadow
Crazy Shadow is the 3D fast-paced and fun puzzle Android game! Try to rotate and drag shapes in the position of their shadows without fail! Solve in succession all combinations of levels of the game.

Learn more...

 

Admin Countdown

Admin Countdown
Module for Joomla! 2.5 and 3.x displays in the administration part of the site, a timer with countdown of the time remaining in your session.

Learn more...  Demo

 

[PHP] Una alternativa a implode()

Abbiamo il semplice quesito seguente:

dato un array di elementi, si vuole produrre una stringa con tali elementi, separando ciascun elemento con una virgola, e senza che la virgola appaia alla fine della lista”.

Cioé, dato per esempio l'array seguente:

$ar_frutti = array("arancia","mela", "limone", "mandarino");


Si vuole ottenere la stringa:

$str_frutti = "arancia, mela, limone, mandarino" ;
La prima cosa che probabilmente viene in mente é la chiamata della funzione seguente, che risponde subito alla richiesta.


$str_frutti = implode(",", $ar_frutti);
Ma se si volesse complicare un poco la richiesta, aggiungendo quanto segue?

inoltre si vogliono prelevare soltanto quegli elementi che iniziano con la lettera 'm' ”.

In questo caso perciò il risultato atteso sarebbe:

 

 

$str_frutti = "mela, mandarino" ;
e quindi, in tal caso, non si potrebbe fare uso della suddetta chiamata a implode().

Tra le diverse soluzioni che si possono escogitare, qui se propone una, semplice ed efficace.

<?
$ar_frutti = array("arancia", "mela", "limone", "mandarino");
$separatore = "";
foreach($ar_frutti as $frutto)
{
	if('m' == $frutto[0])
	{
		$str_frutti .= $separatore.$frutto;
		$separatore = "," ;
	}
}
echo $str_frutti;
?>

Si noti l'uso della parentesi quadra in : $frutto[0], che sta ad indicare l'elemento zero della variabile stringa $frutto, ovvero il primo carattere di tale variabile: questo modo di riferirsi al primo carattere, é preferibile ad altri (ad esempio la chiamata a substr()), perché più rapido.

Infine, per rendere più compatto il codice, si può definire una funzione di filtro, che realizza il controllo suddetto, e che viene passata come argomento alla funzione implode, come segue.

<?
function filtra_array($let,$arr)
{
    foreach($arr as $item)
	if($let == $item[0])
		$ar_filtrato[] = $item;
    return($ar_filtrato);
}
$separatore = ',';
$lettera = 'm';
$arr_frutti = array("arancia", "mela", "limone", "mandarino");
$str_frutti = implode($separatore, filtra_array($lettera, $arr_frutti));
echo($str_frutti);
?>

Ogni contributo é ben accetto: attendiamo il vostro parere con commenti, suggerimenti, critiche!



You have no rights to post comments

Last comments

  • Easy Language: a Joomla Plugin for Multilingual Websites

    • dorcas melda dorcas melda 19.05.2020 13:38
      Purchase healthcare research paper services and healthcare coursework writing services since students ...
  • Passport photo

    • dorcas melda dorcas melda 19.05.2020 13:40
      Among other courses, cultural studies coursework writing services has become popular since students seek ...
  • Switching Light Video

    • dorcas melda dorcas melda 19.05.2020 13:39
      E-commerce research paper writers are hard to come across for those in need of E-commerce Essay Writing ...

Forum Post

  • No posts to display.

Forum Login